首页 > 解决方案 > 为什么 Codeignitor 在验证路由时不接受自动加载控制器类?

问题描述

为什么 Codeignitor 在验证路由时不接受作曲家自动加载中的控制器?

它通过以下方式检查:class_exists($class, FALSE)第二个参数禁用自动加载检查。

https://github.com/bcit-ci/CodeIgniter

    $e404 = FALSE;
    $class = ucfirst($RTR->class);
    $method = $RTR->method;

    if (empty($class) OR ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php'))
    {
        $e404 = TRUE;
    }
    else
    {
        require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php');

        if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method))
        {
            $e404 = TRUE;
        }
        elseif (method_exists($class, '_remap'))
        {
            $params = array($method, array_slice($URI->rsegments, 2));
            $method = '_remap';
        }
        elseif ( ! method_exists($class, $method))
        {
            $e404 = TRUE;
        }
        /**
         * DO NOT CHANGE THIS, NOTHING ELSE WORKS!
         *
         * - method_exists() returns true for non-public methods, which passes the previous elseif
         * - is_callable() returns false for PHP 4-style constructors, even if there's a __construct()
         * - method_exists($class, '__construct') won't work because CI_Controller::__construct() is inherited
         * - People will only complain if this doesn't work, even though it is documented that it shouldn't.
         *
         * ReflectionMethod::isConstructor() is the ONLY reliable check,
         * knowing which method will be executed as a constructor.
         */
        elseif ( ! is_callable(array($class, $method)))
        {
            $reflection = new ReflectionMethod($class, $method);
            if ( ! $reflection->isPublic() OR $reflection->isConstructor())
            {
                $e404 = TRUE;
            }
        }
    }

标签: phpcodeigniterautoload

解决方案


查看 git 历史记录,更改是在49e68de96b420a444c826995746a5f09470e76d9中引入的,提交消息为:

从 class_exists() 事件中禁用自动加载器调用以提高性能

注意:驱动程序库测试似乎依赖于此,因此 CI_Loader 中会出现一次,直到我们解决该问题。

所以名义上的原因是性能。

如果要确保在每个请求上都加载控制器类,可以将文件显式添加到 Composerautoload.files属性,如下所示:

composer.json
{
    "autoload": {
        "files": [
            "src/Foo.php"
        ]
    },
    "name": "test/64166739"
}
src/Foo.php
<?php
class Foo {}
test.php
<?php
$loader = require('./vendor/autoload.php');
var_dump(class_exists('Foo', false));

运行时(php test.php例如通过),我们得到以下输出:

bool(true)

额外的

查看围绕该调用的代码,class_exists看起来控制器文件应该遵循一个约定,例如,对于内置Welcome控制器和默认设置,定义它的文件应该存在于:

application/controllers/Welcome.php

因此,在require_onceing 该文件之后,调用 toclass_exists是一个相当简单的完整性检查,以确保该文件确实定义了该类。因此,基于这种关于控制器如何添加到 CodeIgniter 应用程序的假设(即所有控制器都在application/controllers目录中并且与它们定义的类相同),在执行该检查时绕过自动加载器是合理的。

如果你想确保控制器在需要时加载,CodeIgniter 方式,它们应该被添加到上面列出的应用程序中。


推荐阅读