是否可以在FuelPHP中使用破折号路由所有URL?
在以下配置中,当URL为this-is-a-test/action时,除了指定每个路由使用controller是否可以在FuelPHP中使用破折号路由所有URL?,php,fuelphp,fuelphp-routing,Php,Fuelphp,Fuelphp Routing,在以下配置中,当URL为this-is-a-test/action时,除了指定每个路由使用controllerthisistest外,是否可以使用正则表达式或任何其他方法?我是否需要构建/扩展自己的路由器类 <?php return array( '_root_' => 'home/index', // The default route '_404_' => 'error/404', // The main 404 route //'h
thisistest
外,是否可以使用正则表达式或任何其他方法?我是否需要构建/扩展自己的路由器类
<?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 */
我实现这一点的方法是使用以下内容扩展\Fuel\Core\Router
。router类使用的URI已通过config.php
中的security.URI\u filter
中的方法传递,因此我没有修改router类方法,而是让我的路由器扩展向该数组添加回调
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);
}
}
您可以通过闭包或调用类方法或函数,将其直接添加到app/config/config.php
中的配置数组中
这样做的缺点是/path\u to\u controller/action和/path to controller/action都会工作,并且可能会导致一些重复的内容SEO问题,除非您向搜索蜘蛛指出这一点。这是假设两条路径都在某个地方被引用,即站点地图或
等。我认为router类默认情况下不具备该功能。您确实需要扩展或创建自己的路由器类。您可以使用security.uri\u过滤器配置设置
创建一个将连字符转换为下划线的函数,就完成了。您不需要为此扩展router类。只需将函数名(无论是在类中还是在引导中定义的函数中)提供给配置,您就可以退出了。我知道这是在事件之后,但这是为以后想要的任何其他人准备的
为了避免下划线和子文件夹之间的混淆,我倾向于将连字符转换为驼峰大小写,因此将URLthis-is-a-test
路由到classController\u thisistest
为此,我(在FuelPHP 1.4中)在fuel/app/config/config.php
中的“安全”设置中向“uri_过滤器”添加了一个匿名函数:
'security' => array(
'uri_filter' => array('htmlentities',
function($uri) {
return str_replace(' ', '', ucwords(str_replace('-', ' ', $uri)));
}),
),
评论你的“缺点”。除非我弄错了,否则蜘蛛不会知道/path\u to\u controller/action
(带下划线),除非有指向它的链接或在站点地图中。有趣的。。。我会调查的。