1

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

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

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

这怎么可能在codeigniter中使用路由?

4

3 回答 3

1

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

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

/application/admin/config/routes.php

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

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

$route['404_override'] = 'default_page'; 
于 2013-01-22T11:03:56.227 回答
1
  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';
于 2013-01-23T04:04:36.627 回答
0

您想要的是 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";
于 2013-01-22T11:13:12.053 回答