在以下配置中,除了指定每个路由使用 Controller thisisatest
之外,是否可以使用正则表达式或任何其他方法,当 URL 为 this-is-a-test/action
?我必须构建/扩展我自己的 Router 类吗?
<?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
使用以下内容。路由器类使用已通过 security.uri_filter
中的方法传递的 URI。来自 config.php
因此,我没有修改路由器类方法,而是让我的路由器扩展向该数组添加了一个回调。
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_to_controller/action 和/path-to-controller/action 都可以工作,并且可能会导致一些重复的内容 SEO 问题,除非您向搜索蜘蛛指出这一点。这是假设在某处引用了两个路径,即站点地图或 <a href="">
等
关于php - 是否可以在 FuelPHP 中路由所有带有破折号的 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11652447/