Kohana 3.2 routing issue

107 views Asked by At

I have set up my routes so that most classes are called in the standard controller/action style. However for my front end I don't want users to see the action being called, so all front end pages have their own controller and use the index action. These are my routers in bootstrap:

Route::set('normal', '<controller>(/<action>(/<arguments>))', 
    array(
        'arguments'     => '.*'
    ))
    ->defaults(array(
        'controller'    => 'admin',
        'action'        => 'index',
    ));

Route::set('default', '(<controller>(/<arguments>))', 
    array(
        'arguments'     => '.*',
    ))
    ->defaults(array(
        'controller'    => 'home',
        'action'        => 'index',
    ));

I currently have three front end pages, home, about_us and services. They all work great if I don't pass any arguments in through the URL, but the problem occurs if I try to pass an argument in through the URL into arguments. In services there is only action_index() to display the page and it checks for any arguments, and displays results based on the argument. However if I try to browse to /services/1 to pass in 1 as an argument I get this 404 error:

Kohana_HTTP_Exception [ 404 ]: The requested URL services/1 was not found on this server.

It just uses the first route, normal. Once it can't find the action it doesn't even try to use the second route, which would work. If I swap the order of the routes then it works, but all my other classes that use the first controller stop working as all my actions get passed to action_index() as arguments.

How can I get this working? Why when the first route doesn't work does Kohana not go on to the second route?

1

There are 1 answers

1
mobal On

You do not need to create controller per route. You can create custom routes (route names) and specify controller and action name. Read the documentation for further explanation. Routing

For arguments try this:

Route::set('default', '(<controller>(/<arguments>))', 
    ->defaults(array(
        'controller'    => 'home',
        'action'        => 'index',
    ));