TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
KO3 Catch all route?
  • Hi, I was wondering, what is the best way to apply a catch-all route? For example, I have 2 controller called pages and gallery.

    If someone were to visit /gallery, the routes will reroute to the gallery controller.
    However, if someone visited /contact or /about, the routes will reroute to the pages controller. In the pages controller, I'll do something like call the DB for the page view.

    So, my question is, what is the best way to implement this? I've tried 2 different methods:

    The first is setting the routes regex and manually setting the pages that exists in the DB like:


    Route::set('pages', '(<id>)', array(
    '(contact|about)' // <== Manually edit this
    ))
    ->defaults(array(
    'controller' => 'pages',
    'action' => 'view',
    'id' => 'index',
    ));


    However, I don't really like to do it manually. I have another idea of try-catch the Request instance but I'm not sure how to implement this.

    Any ideas?
  • Have your gallery as the exception:

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

    Then catch all and route to page:

    Route::set('pages', '<page>', array('page' => '.*'))
        ->defaults(array(
            'controller' => 'page',
            'action' => 'view',
            'page' => 'index',
        ));
    

    Or have I misunderstood what you want?

  • hmm yes, that's what I want. However, I would have to enter an entry for every controller I have. So, I'm thinking of a different method...
  • A slightly hacky work around would be to build your routes based on the controllers found in the application so use Kohana::list_files('classes/controller') the parse the controller names and create routes in bootstrap. Not very efficient though - I guess you could replace it with manual code once it was live.

    As it is there is no way to specify routes that actually check whether they are valid. If you could somehow add a callback to check if the controller (or some other arbitrary bit of data) exists that would be really useful in situations like this.

    You could always extend Route...

  • What if you create a main controller, wich checks in function before() for valid routes.
    And your controllers can extend the main controller.
  • @zsolt:

    You would then have to route everything to the main controller and issue sub-requests for the actual controller/action desired. I am considering this myself for a project but it seems to be such fundamental behaviour for so many on the forums recently it would be great to have it built in to Route.

    Even if all route did was check if a controller existed as part of matching a route and if it doesn't return false to the match allowing request to go on and try the next route.

    Perhaps there is some good reason not to do it like this but it would be good to at least have it as an option.

  • I've extended the Kohana_Request Class.
    Changed the Method exeute():
    if ( ! $this->_route instanceof Route){

        URL::accessDenied('Your requested Page does not exists.');
    
        /*
        return HTTP_Exception::factory(404, 'Unable to find a route to match the URI: :uri', array(
        ':uri' => $this->_uri,
               ))->request($this)
        ->get_response();
        * 
        */
    

    }
    The URL Class I've extended too with my own made accessDenied Method which exits for a redirect.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion