1

我目前正在研究 PHP 框架 Codeigniter 并了解到目前为止的主要概念,直到关于 _remapping 的控制器部分。我了解 _remapping 如何通过 URI 覆盖控制器方法的行为,例如从 www.example.com/about_me 到 www.example.com/about-me。我想听的是人们对使用什么的意见-_remapping 方法或 URI Routing 方法?我只是在研究这些方法时才问这个问题,并且有人对重新映射功能感到困扰,他们被指示使用 URI 路由。

所以..

1) 主要常用方法是什么?2) PHP5 CI 版本 2 以后最好使用 URI 路由吗?

听听您的意见,我将不胜感激!

4

3 回答 3

1

如果要更改默认 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() 方法。

于 2012-08-20T10:33:15.687 回答
1

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

$route['category/(:any)'] = 'category/view/$1';

然后您只需要在您的类别控制器中执行一个查看操作来接收类别名称,即 PHP。

http://www.yourdomain.com/category/PHP

function View($Tag)
{
    var_dump($Tag);
}

如果您仍想在控制器中访问索引操作,您仍然可以通过http://www.yourdomain.com/category/index访问它

于 2012-08-20T12:16:56.390 回答
1
$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';
于 2017-08-22T10:56:55.343 回答