Created My First Kohana Route

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:

http://ff2fb.lysender.co.cc/index/index/4

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:

http://ff2fb.lysender.co.cc/index/4

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.

This entry was posted in Kohana v3 and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *