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

时间:2013-01-22 11:00:39

标签: php codeigniter url-rewriting routing

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

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

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

如何在codeigniter中使用路由?

3 个答案:

答案 0 :(得分:1)

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

您可以在

中找到路线位置

/application/admin/config/routes.php

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

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

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

答案 1 :(得分:1)

  1. 将路线添加到“/ project /...”
  2. 下的所有现有控制器
  3. 添加与“/ project”
  4. 下的任何路径匹配的路由

    示例:

    /* 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';
    

答案 2 :(得分:0)

你想要的是虚荣网址,你可以在这里找到一个在代码点火器中执行此操作的指南:

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