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

In the following configuration, you can use a regular expression or any other method than specifying each route to use thisisatest controller when the this-is-a-test/action ? Should I create / 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 */ 
+4
source share
4 answers

The way I implemented this was to extend \Fuel\Core\Router using the following. The router class works with a URI that passed through the methods from security.uri_filter from config.php , so instead of modifying the methods of the router class, I had an extension of my router, add a callback to this 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 can simply add it directly to the configuration array in app/config/config.php by closing or calling a class or function method.

The disadvantage of this is that both the actions / path _to_controller / action and / path-to-controller / action will work and possibly cause some problems with duplicate content if you do not specify this to the search spider. This assumes that both paths link somewhere, for example, to a sitemap or to <a href=""> , etc.

+4
source

I believe that the router class does not have functionality by default. You really need to expand or create your own router class.

+1
source

You can use the security.uri_filter configuration setting for this.

Create a function that converts hyphens to underscores, and you're done. You do not need to extend the class of the router. Just put the name of the function (in the class or function defined in the bootstrap) in the configuration and you are disabled.

0
source

I know this after the event, but it is for those who want this in the future ...

To avoid confusion between underscores and subfolders, I preferred converting hyphens to camel case, so the routing URL of this-is-a-test in the Controller_ThisIsATest class.

I did this (in FuelPHP 1.4) by adding an anonymous function to uri_filter in the security settings in fuel/app/config/config.php :

 'security' => array( 'uri_filter' => array('htmlentities', function($uri) { return str_replace(' ', '', ucwords(str_replace('-', ' ', $uri))); }), ), 
0
source

All Articles