我目前正在研究PHP框架Codeigniter,并理解到目前为止的主要概念,直到关于_remapping的控制器部分。我对_remapping如何通过URI重写控制器方法的行为有所了解,例如从www.example.com/about_me重写到www.example.com/about-me。我想听到的是人们对使用_remapping方法还是URI路由方法的意见?我之所以问这个问题,是因为当研究这些方法时,有人在重新映射功能上遇到了麻烦,他们被指示使用URI路由。
所以..。
1)使用的主要通用方法是什么,pro优于其他方法? 2)对于PHP5 CI版本2及以后的版本,最好使用URI路由吗?
如果能听到您的意见,我将不胜感激!
发布于 2012-08-20 20:16:56
假设您不想使用Categories控制器的index (即http://www.yourdomain.com/category)操作,则可以使用路由。
$route['category/(:any)'] = 'category/view/$1';然后,您只需要在Category控制器中使用View操作来接收Category名称,即PHP。
http://www.yourdomain.com/category/PHP
function View($Tag)
{
var_dump($Tag);
}如果您仍然希望在控制器中访问索引操作,则仍然可以通过http://www.yourdomain.com/category/index访问它
发布于 2012-08-20 18:33:16
如果要更改默认配置项的路由行为,则应使用_remap。
例如,如果您设置了维护并希望阻止任何特定控制器运行,则可以使用_remap()函数加载视图,该函数不会调用任何其他方法。
另一个例子是当URI中有多个方法时。示例:
site.com/category/PHP
site.com/category/Javascript
site.com/category/ActionScript你的控制器是category,但是方法是无限的。在这里,您可以使用Colin Williams在此处调用的_remap方法:http://codeigniter.com/forums/viewthread/135187/
function _remap($method)
{
$param_offset = 2;
// Default to index
if ( ! method_exists($this, $method))
{
// We need one more param
$param_offset = 1;
$method = 'index';
}
// Since all we get is $method, load up everything else in the URI
$params = array_slice($this->uri->rsegment_array(), $param_offset);
// Call the determined method with all params
call_user_func_array(array($this, $method), $params);
} 总而言之,如果当前配置项的路由适合您的项目,就不要使用_remap()方法。
发布于 2017-08-22 18:56:55
$default_controller = "Home";
$language_alias = array('gr','fr');
$controller_exceptions = array('signup');
$route['default_controller'] = $default_controller;
$route["^(".implode('|', $language_alias).")/(".implode('|', $controller_exceptions).")(.*)"] = '$2';
$route["^(".implode('|', $language_alias).")?/(.*)"] = $default_controller.'/$2';
$route["^((?!\b".implode('\b|\b', $controller_exceptions)."\b).*)$"] = $default_controller.'/$1';
foreach($language_alias as $language)
$route[$language] = $default_controller.'/index';https://stackoverflow.com/questions/12035955
复制相似问题