_代码点火器中的remap或URI路由


_remap or URI Routing in codeigniter

我目前正在研究PHP Framework Codeigniter,并了解到目前为止的主要概念,直到Controllers部分介绍_remapping为止。我了解_remapping如何在URI上覆盖控制器方法的行为,例如从www.example.com/about_me到www.example.com/about-m。我想听听人们对使用-remapping方法或URI路由方法的意见?我只是问这个问题,因为在研究这些方法时,有人在重新映射函数时遇到了麻烦,他们被指示使用URI路由。

所以。。

1) 最常用的方法是什么?专业人士的方法比其他方法好?2) 对于PHP5 CI版本2以后的版本,是否最好使用URI路由?

如果能听听你的意见,我将不胜感激!

假设您不想使用index(即。http://www.yourdomain.com/category)您的Categories控制器的动作,您可以使用路由。

$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

如果要更改默认CI路由的行为,则应使用_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);
}  

总之,如果当前CI的路由适合您的项目,则不要使用_remap()方法。

$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';