Reputation: 3798
I have multiple controllers in my Kohana 3.2 project where the routes where initially:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
It was working fine for all my new controllers (when I added a new file and went to: domain/controller it worked like a charm.
Now for a specific controller called parents I had to add new lines in my bootstrap:
Route::set('parents', '(<controller>(/<action>))')
->defaults(array(
'controller' => 'parents',
'action' => 'index',
));
Route::set('parent', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'parent',
'action' => 'index',
));
I was trying to access both: /parents/ and /parent/index/id and both generated an error when not having the Route::set
in place.
Without those lines, I always got errors like:
unable to find a route to match the uri
OR
The requested route does not exist
How am I supposed to do it? For every controller that I add do I need to define it in my bootstrap?
Upvotes: 1
Views: 713
Reputation: 1128
Actually you'd be covered with just the default route in your case.
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'user',
'action' => 'index',
));
First, Kohana tries to match against your regex pattern [((/(/)))]. This will match the urls: users, users/delete, users/delete/1, parents, parents/view, parents/view/2, etc.
If Kohana is unable to find the action, it will default to index based on your defaults array rule. If Kohana is unable to find a controller (which essentially means nothing passed), then it will use controller. In the last case, it would also default the action since we can't pass an action without passing a controller in our regex (see the parenthesis requires a controller first then action then id).
So the following examples will route through this default pattern:
URL: /users
Controller: users
Action: index (picked default)
URL: /users/kill/1
Controller: users
Action: kill
Id: 1
URL: /parents
Controller: parents
Action: index (picked default)
URL: /parent/view
Controller: parent
Action: view
Best practice is to keep this route as the last route applied (essentially the default and catch all) and if you have urls that don't match the pattern in the default, add them.
Upvotes: 1
Reputation: 17000
I'd better use one route for all this:
Route::set('p', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'action' => 'index',
));
If you call /parents/father
- you get to parents
controller, if you call /parent/father
you get to parent
Upvotes: 1