如果编码器点火器中不存在控制器,请重写 URL


Rewrite URL if controller is not existing in codeigniter

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

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

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

如何在代码点火器中使用路由来实现这一点?

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

您可以在以下位置找到路线位置

/

application/admin/config/routes.php

$route['default_controller'] = "welcome";

在找不到页面的情况下也使用以下

$route['404_override'] = 'default_page'; 
  1. 将路由添加到"/project/..." 下的所有现有控制器
  2. 添加与"/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';
你想要

的是虚荣的URL,你可以在这里找到在代码点火器中执行此操作的指南:

http://philpalmieri.com/2010/04/personalized-user-vanity-urls-in-codeigniter/

本质上,您正在将其添加到路由文件中:

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