Recently, I have tried to create my own route in Kohana v3. It is a very simple route for a paginated feed streams from a social network. My previous method was to just use the first parameter “id” as the page but my URL looks ugly like this:
Where 4 there is the page number. It maps to my main page’s index controller and index action. I want to remove the action part so that my URL will simply look like this:
Still ugly, but way better. So here is my route at bootstrap.php:
/** * Router for main page friendfeed streams with better url * Default and only action is index, and the next parameter * is the page number */ Route::set('index', 'index(/<page>)') ->defaults(array( 'controller' => 'index', 'action' => 'index' ));
That route must be placed before the default route. On my index controller, retrieving the page parameter is as simple as:
$page = $this->request->param('page');
If no parameter is specified (ex: page 1), page value is null.
Small code for small changes.