Can someone explain Kohana 3's routing system?
Asked Answered
V

4

9

In bootstrap.php, where you set routes, I'm having a hard time getting them to work. I read some documentation a while ago that I can't seem to find again that explains them. Here is one of my examples

Route::set('products', 'products/(type)', array('type' => '.+'))
    ->defaults(array(
    'controller' => 'articles',
    'action' => 'view_product',
    'page' => 'shock-absorbers',
    ));

I thought that would mean a request like products/something would load up the articles controller, and the action_view_product() method. But I can't get it to work.

Can someone please explain to me exactly how they work, and what all the method parameters are?

Valer answered 19/1, 2010 at 2:52 Comment(0)
A
5

I thought that would mean a request like products/something would load up the articles controller, and the action_view_product controller. But I can't get it to work.

You got the bolded part wrong. It will actually load action_view_product method of the articles controller:

class Controller_Articles extends Controller {
   public function action_view_product() {
       $params = $this->request->param(); 
       // if the uri is `products/something' then $params['type'] == 'something'
   }
}

EDIT:

Oh my god oh your god why didn't I notice!!!

The actual problem lies within your route pattern! It should have been products/(<type>), with the angle brackets. Those will hint Kohana that you intended the 'type' to be a parameter name, instead of a literal.

Asuncion answered 19/1, 2010 at 3:6 Comment(3)
Sorry, that was a typo! I still can't seem to get it to work for me.Valer
Yes, I have a default route that is capturing all (and working alright)Valer
did you declare this custom route before the default route? because the routing will match the first route it encounters, and since the default route will match 'products/something', it will take that route.Asuncion
H
3

uff, sorry, lower then and greater then signs doesn't shows correctly

'products/(type)' should be 'products/(<type>)'
Hindermost answered 19/1, 2010 at 9:2 Comment(1)
or even better: 'product(/<type>)', because otherwise 'product' (without slash) wouldn't be matched (although you might want it that way)Stolon
K
3

The parentheses indicate optional parts (the regex will match if they are missing). These can be static and/or contain named variables. The angle brackets indicate a named variable in the route which is accessible in the controller via:

$this->request->param('type');

I wrote the official routing guide which you can read here, it should answer all of your questions.

Kasten answered 19/1, 2010 at 18:41 Comment(0)
K
0

For the record:

The directory, controller and action can be accessed from the Request as public properties like so:

// From within a controller:
$this->request->action;
$this->request->controller;
$this->request->directory;

// Can be used anywhere:
Request::instance()->action;
Request::instance()->controller;
Request::instance()->directory;

source: http://kohanaframework.org/3.0/guide/kohana/routing#request-parameters

Kali answered 21/5, 2013 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.