如果用户试图访问任何不存在的控制器,则重写 URL。
例如:- 如果用户尝试访问http://example.com/project/anyvalue
. 在我的程序中没有名称为“anyvalue”的控制器。在这种情况下,我想重定向到
http://example.com/project/profile/anyvalue
这怎么可能在codeigniter中使用路由?
如果用户试图访问任何不存在的控制器,则重写 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';
例子:
/* 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';
您想要的是 Vanity 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";