phpfuelphpfuelphp-routing

Is it possible to route all URLs with dashes in FuelPHP?


In the following configuration is it possible to use a regular expression or any other method besides specifing each route to use controller thisisatest when URL is this-is-a-test/action? Would I have to build/extend my own Router class?

<?php
return array(
    '_root_'  => 'home/index',  // The default route
    '_404_'   => 'error/404',    // The main 404 route

    //'hello(/:name)?' => array('welcome/hello', 'name' => 'hello')
);

/* end of config/routes.php */

Solution

  • The way I implemented this was to extend \Fuel\Core\Router using the following. The router class works with a URI which has been passed through the methods in security.uri_filter from config.php so rather than modifying the router class methods I had my router extension add a callback to that array.

    class Router extends \Fuel\Core\Router
    {
        public static function _init()
        {   
            \Config::set('security.uri_filter', array_merge(
                \Config::get('security.uri_filter'),
                array('\Router::hyphens_to_underscores')
            ));
        }
    
        public static function hyphens_to_underscores($uri)
        {
            return str_replace('-', '_', $uri);
        }
    }
    

    You could just as easily add it straight to the configuration array in app/config/config.php by way of a closure or a call to a class method or a function.

    The downside of this is that both /path_to_controller/action and /path-to-controller/action will work and possibly cause some duplicate content SEO problems unless you indicate this to the search spider. This is assuming both paths are referenced somewhere i.e. a sitemap or an <a href=""> etc.