Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果codeigniter中不存在控制器,请重写URL_Php_Codeigniter_Url Rewriting_Routing - Fatal编程技术网

Php 如果codeigniter中不存在控制器,请重写URL

Php 如果codeigniter中不存在控制器,请重写URL,php,codeigniter,url-rewriting,routing,Php,Codeigniter,Url Rewriting,Routing,如果用户试图访问任何不存在的控制器,请重写URL 示例:-如果用户试图访问http://example.com/project/anyvalue。在我的程序中,没有名为“anyvalue”的控制器。在这种情况下,我想重定向到 http://example.com/project/profile/anyvalue 如何使用codeigniter中的路由实现这一点?如果缺少控制器,请使用默认路由将请求重定向到某个特定页面 您可以在中找到路线位置 /application/admin/config/

如果用户试图访问任何不存在的控制器,请重写URL

示例:-如果用户试图访问
http://example.com/project/anyvalue
。在我的程序中,没有名为“anyvalue”的控制器。在这种情况下,我想重定向到

http://example.com/project/profile/anyvalue

如何使用codeigniter中的路由实现这一点?

如果缺少控制器,请使用默认路由将请求重定向到某个特定页面

您可以在中找到路线位置

/application/admin/config/routes.php

$route['default_controller'] = "welcome";
如果找不到页面,也请使用以下命令

$route['404_override'] = 'default_page'; 

您需要的是虚荣URL,您可以在下面的代码点火器中找到执行此操作的指南:

基本上,您要将其添加到路由文件中:

$handle = opendir(APPPATH."/modules");
while (false !== ($file = readdir($handle))) {
  if(is_dir(APPPATH."/modules/".$file)){
    $route[$file] = $file;
    $route[$file."/(.*)"] = $file."/$1";
  }
}

/*Your custom routes here*/

/*Wrap up, anything that isnt accounted for pushes to the alias check*/
$route['([a-z\-_\/]+)'] = "aliases/check/$1";
  • 将路由添加到“/project/…”下的所有现有控制器
  • 添加与“/project”下的任何路径匹配的路由
  • 例如:

    /* Currently available controllers under "/project/" */
    $route['project/profile'] = "project/profile";
    $route['project/add'] = "project/add";
    $route['project/edit'] = "project/edit";
    
    /* Catch all others under "/project/" */
    $route['project/(:any)'] = "project/profile/$1";
    
    /* if controller class name is Profile and function name is index */
    $route['project/(:any)'] = 'project/profile/index/$1';
    

    我很确定这只针对文档的根,而不是缺少的控制器。如果用户试图访问,那么我需要重定向到。这是项目简介页面。我不需要重定向到默认控制器。感谢您的支持。更新了ans以涵盖所有可能性。。谢谢你的意见@RyanMcDonoughTry在本用户指南中使用“URI路由”我可以像这样设置路由$route['project/^(?!other | controllers.*']='project/profile/$1';