简体   繁体   English

CodeIgniter 3没有调用默认控制器?

[英]CodeIgniter 3 not calling default controller?

I have been asked to look into an issue where the default_controller in a CodeIgniter 3 project does not seem to be called. 我被要求调查一个似乎未调用CodeIgniter 3项目中的default_controller的问题。 Instead we are seeing a 404 error. 相反,我们看到404错误。

In the application/controllers folder there is a Welcome.php file with the following content: application/controllers文件夹中,有一个Welcome.php文件,其内容如下:

class Welcome extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                // Your own constructor code
        }

        public function index()
        {
                print('hello');
                $this->load->view('welcome_message');
        }
}

The application/config/routes.php file has: application/config/routes.php文件具有:

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

I only see a 404 and none of the expected text. 我只看到404,但没有看到预期的文字。

Adding a print statement to the routes.php shows that it is being loaded. 将一条print语句添加到routes.php表明它正在被加载。 Also, tying it explicitly to a route has it called, but not when it is set to be the default controller. 同样,将其显式绑定到路由会调用它,但是将其设置为默认控制器时不会调用它。

$route['blah'] = "welcome"

Can anyone suggest what may be going on? 谁能建议可能会发生什么?

BTW We are using PHP7 on an Ubuntu 16.04 machine. 顺便说一句,我们在Ubuntu 16.04机器上使用PHP7。

Turns out the project was an upgrade from a CodeIgniter 2 project and there were some migration steps that hadn't quite been completed. 原来,该项目是CodeIgniter 2项目的升级,并且其中一些迁移步骤尚未完全完成。 It turned out that there was a MY_Router.php in the libraries folder, that seemed to be throwing things off - at least moving it into applications/core solved the issue. 原来,在libs文件夹中有一个MY_Router.php,这似乎很麻烦-至少将其移入应用程序/核心可以解决此问题。

An additional modification was needed in the MY_Router.php file given the capitalization of the filenames, requested as part of the CI3 migration: 给定文件名的大写形式,需要在MY_Router.php文件中进行其他修改,作为CI3迁移的一部分:

function _validate_request($segments)
{
    // Does the requested controller exist in the root folder?
    if (file_exists(APPPATH.'controllers/'.ucfirst($segments[0]).'.php'))
    {
        return $segments;
    }
    ...
}

BTW I was sure it wasn't a HTTP server rewrite issue, since a CodeIgniter 404 error page was showing the expected call path. 顺便说一句,我确定这不是HTTP服务器重写问题,因为CodeIgniter 404错误页面显示了预期的调用路径。 I had added a print(_SERVER['REQUEST_URI']) to see what was going on. 我添加了一个print(_SERVER['REQUEST_URI'])以查看发生了什么。

Edit: I now see that ucfirst approach above is not ideal in other installations, but given that there are no subfolders in this project's controllers folder, it is a quick fix for this project. 编辑:我现在看到上面的ucfirst方法在其他安装中并不理想,但是鉴于该项目的controllers文件夹中没有子文件夹,因此可以快速解决该项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM