重定向到控制器的路由+;CodeIgniter上的默认操作?

重定向到控制器的路由+;CodeIgniter上的默认操作?,codeigniter,controller,routes,codeigniter-url,Codeigniter,Controller,Routes,Codeigniter Url,我目前正在与Codeigniter合作一个项目 我有一个控制器叫Cat class Cat extends CI_Controller { function __construct(){ parent::__construct(); } function index($action){ // code here } } 和一个路由(在routes.php中) 如果我使用这个URL,这是可行的,例如: 然而,如果用户将URL更

我目前正在与Codeigniter合作一个项目

我有一个控制器叫Cat

class Cat extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function index($action){
        // code here
    }

}
和一个路由(在routes.php中)

如果我使用这个URL,这是可行的,例如:

然而,如果用户将URL更改为,它将不再工作。Codeigniter写入:404未找到页面-未找到您请求的页面

因此,我的目标是在默认情况下将他重定向到cats/页面

我需要走另一条路线吗? 我试过了


……但没有成功。谢谢您的帮助。

有几种方法可以做到这一点:

默认情况下,您可以为$action提供“显示”:

function index($action = 'display'){}

您可能有条件对其进行物理重定向

function index($action = ''){
   if(empty($action)){redirect('/cats/display');}
   //OTher Code
}

您需要在无任何内容时提供路线:

$route['cats'] = 'cat/index/display'; //OR the next one
$route['cats'] = 'cat/index'; //This requires an function similar to the second option above
此外,如果您的路线中只有特定数量的选项(即“显示”、“编辑”、“新建”),则可能需要按如下方式设置路线:

$route['cats/([display|edit|new]+)'] = 'cat/index/$1';
编辑:

您创建的上一条管线:

$route['cats'] = 'cat/display';

实际上是在控制器中查找
函数display()
,而不是通过索引“display”选项在控制器中使用“重新映射”函数的最佳方法它会将您的url重新映射到控制器的特定方法

class Cat extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function _remap($action)
    {
       switch ($action)
       {
            case 'display':
             $this->display();
            break;
            default:
               $this->index();
            break;
        }
    }

    function index($action){
        // code here
    }

    function display(){
        echo "i will display";
    }
    }

$route['cats'] = 'cat/display';
class Cat extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    function _remap($action)
    {
       switch ($action)
       {
            case 'display':
             $this->display();
            break;
            default:
               $this->index();
            break;
        }
    }

    function index($action){
        // code here
    }

    function display(){
        echo "i will display";
    }
    }