Kohana 3.0 Using parameters in default route

79 Views Asked by At

I'm trying to use my index controller to create a url structure like this: mydomain.com/vehiclemake/vehiclemodel/vehiclemodelyear

I don't know how to alter the default route, or alter a duplicate that will work as intended. Every time I load the page once a make has been added to the url, it gives me a blank screen and the logs tell me it can't find a controller with the name of the vehicle make that was in the url. Below is the default and what I've tried.

Route::set('vehicle', '(/<make>(/<model>(/<model_year>)))')
    ->defaults(array(
        'controller' => 'index',
        'action'     => 'index',
    ));
Route::set('default', '(<controller>(/<action>(/<id>)))')
    ->defaults(array(
        'controller' => 'index',
        'action'     => 'index',
    ));

I've tried looking for an answer here on stackoverflow but I haven't found a question that's similar to mine that has an answer.

2

There are 2 best solutions below

1
mrBrown On

I'm not sure if I understand your question right. Anyway, you won't need the first / before .

Besides that, this structure will require a controller for every make and in that controller an action for every model. I think something like the code below will work for the uri: /ferrari/testarossa/1992

class Ferrari extends controller {
  public function action_testarossa() {
     // whatever you'd like to do
     echo $this->request->param('model_year');
  }
}

I think the default in routing always is, but I'm not sure. 1. controller 2. action 3. whateveryoudefine 4. whateveryoudefine

Hope this helps!

0
bato3 On

You must plan your routing so that one URL cannot be matched to 2 routes. I suggest:

Route::set('vehicle', '/<make>(/<model>(/<model_year>))')
    ->defaults(array(
        'controller' => 'vehicle',
        'action'     => 'index',
    ));

Route::set('mainpage', '/')
    ->defaults(array(
        'controller' => 'index',
        'action'     => 'index',
    ));

Route::set('default', '<controller>(/<action>(/<id>))', ['controller'=>'(index|user)'])
    ->defaults(array(
        'controller' => 'index',
        'action'     => 'index',
    ));

Note that I removed 1 pair of brackets. And for default route correct controllers are: index and user.