当在 Codeigniter _remap() 函数中找不到函数时重定向到 index() 函数


Redirection to index() function when function not found in Codeigniter _remap() function

当我做这样的事情时,我有一个错误:

public function _remap()
    {
        $firstURISegment = $this->uri->segment(1);
        $secondURISegment = $this->uri->segment(2);
        if($firstURISegment == "user")
        {
            if($secondURISegment == "")
            {
                echo "USER INDEX";
            }
            else
            {
                $this->$secondURISegment();
            }                
        }
        else
        {
            echo $firstURISegment;               
        }
    } 

当我调用一个未由此www.example.com/user/abcd在类中定义的函数时,我会收到此错误。

Fatal error: Call to undefined method user::abcd() in C:'xampp'htdocs'livears'application'controllers'user.php on line 17
A PHP Error was encountered
Severity: Error
Message: Call to undefined method user::abcd()
Filename: controllers/user.php
Line Number: 17
Backtrace:

我想重定向到此类的index()函数,而不是此错误。我该怎么做?请看,我已经这样做了:

$route[':any'] = 'user/$1';

routes.php是为了获得www.example.com/username而不是www.example.com/user/username.由于我的 User 类还有其他函数,我已经重新映射以调用这些函数,但正如您所看到的,如果我调用一个未定义的函数,我会得到一个Fatal error。请帮助我重定向。

首先,控制器的名称应以大写字母开头

您的_remap没有执行任何重定向操作。像这样使用它

public function _remap()
    {
        $firstURISegment = $this->uri->segment(1);
        $secondURISegment = $this->uri->segment(2);
        if($firstURISegment == "user")
        {
            if($secondURISegment == "")
            {
                echo "USER INDEX";
            }
            else
            {
                If ( function_exists ($secondURISegment))
             {  $this->$secondURISegment();}
              else {
                  redirect('index');
                   }
            }                
        }
        else
        {
            echo $firstURISegment;               
        }
    } 

你明白吗?现在在你的代码中实现它?