【问题标题】:CodeIgniter 3 not calling default controller?CodeIgniter 3 不调用默认控制器?
【发布时间】:2017-02-19 03:10:32
【问题描述】:

我被要求调查 CodeIgniter 3 项目中的 default_controller 似乎没有被调用的问题。相反,我们看到的是 404 错误。

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');
        }
}

application/config/routes.php 文件有:

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

我只看到 404,没有看到预期的文本。

print 语句添加到routes.php 表明它正在被加载。此外,将其显式绑定到路由会调用它,但不会在将其设置为默认控制器时调用。

$route['blah'] = "welcome"

谁能建议可能发生的事情?

顺便说一句,我们在 Ubuntu 16.04 机器上使用 PHP7。

【问题讨论】:

  • 你的.htaccess文件呢?
  • 怎么样?我应该检查什么?
  • 使用localhost/project_name/welcome。有什么错误吗?
  • @AndreM 有什么更新吗?解决了吗?
  • @AndreM 如果它没有 CI 所期望的标准规则,即重写为 index.php,那么这将失败。

标签: php codeigniter codeigniter-3


【解决方案1】:

原来该项目是对 CodeIgniter 2 项目的升级,并且有一些迁移步骤尚未完全完成。事实证明,在库文件夹中有一个 MY_Router.php,这似乎把事情搞砸了——至少把它移到应用程序/核心解决了这个问题。

鉴于文件名的大写,需要在 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;
    }
    ...
}

顺便说一句,我确信这不是 HTTP 服务器重写问题,因为 CodeIgniter 404 错误页面显示了预期的调用路径。我添加了一个print(_SERVER['REQUEST_URI']) 来看看发生了什么。

编辑:我现在看到上面的 ucfirst 方法在其他安装中并不理想,但鉴于此项目的控制器文件夹中没有子文件夹,它是此项目的快速修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多