Composer 自动加载文件不起作用 [英] Composer autoload file not working

查看:84
本文介绍了Composer 自动加载文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自动加载规范如下

 "自动加载" : {psr-4":{"MyMVC\" : "app/"},类图":["应用程序/控制器",应用程序/助手"],文件":[app/routes.php"]},

routes.php 文件内容为:

add('/', 'HomeController@index');$route->add('about', 'AboutController@index');$route->add('contact', 'ContactController@index');

现在在我的 app/init.php 我试图使用 $route 对象,但它给了我错误

Notice: Undefined variable: route in/var/www/html/mymvc/app/init.php on line 29

这是我尝试使用 $route 对象的方式.

/*** 构造函数* 根据提供的配置引导我们的应用程序*/公共函数 __construct(){//require 'app/routes.php` 这可以正常工作,但应该自动加载var_dump($route);出口;}

我还运行了命令 composer dump-autoload

解决方案

自动加载在此处不起作用.PHP 只能自动加载类.您期望 app/routes.php 将自动加载是不可能的,因为该文件不包含类声明,并且您无法使用以前未知的类来触发它的执行.>

当您包含 vendor/autoload.php 时,Composer 确实会执行一次该文件 - 但是,这确实是您软件的不良行为.不要使用文件"自动加载来包含配置文件.请注意这在库中使用时可能产生的性能影响.您应该完全避免使用它,它旨在用于无法通过其他方式工作的遗留代码.

另一方面,您的架构已损坏.您不应该编写一个仅通过访问应该在其他地方初始化的变量来神奇地"了解配置的类.一个好的模式是将配置作为参数传递给构造函数:

公共函数__construct ($routes){$this->routes = $routes;}

创建此类的代码部分应该从某处获取配置并将其作为参数传递.这个概念被称为控制反转或依赖注入:类不会调用它们需要使用的其他类,它们会请求它们并将它们作为参数获取.

My Autoload specification are as follows

  "autoload" : {
        "psr-4" : {
            "MyMVC\" : "app/"
        },
        "classmap": [
            "app/Controllers",
            "app/Helpers"
        ],
        "files": ["app/routes.php"]
    },

The contents of routes.php file are:

<?php
use MyMVCCoreRoute;

$route = new Route;
$route->add('/', 'HomeController@index');
$route->add('about', 'AboutController@index');
$route->add('contact', 'ContactController@index');

now in my app/init.php i am trying to use the $route object but its giving me error

Notice: Undefined variable: route in /var/www/html/mymvc/app/init.php on line 29

Here is how i am trying to use the $route object.

/**
 * Constructor
 * Bootstrap our application based on the configurations provided
 */
public function __construct()
{
    // require 'app/routes.php` This will work fine but it should be autoloaded
    var_dump($route);
    exit;
}

I have also ran command composer dump-autoload

解决方案

Autoloading won't work here. PHP can only autoload classes. Your expectation that app/routes.php will be autoloaded is not possible, because that file does not contain a class declaration, and you are not able to trigger it's execution by using a previously unknown class.

It is true that Composer will execute that file once when you include vendor/autoload.php - however, this is really bad behavior of your software. Don't use the "files" autoloading to include configuration files. Mind the performance impact this may have when being used in libraries. You should avoid using it altogether, it is meant to be used for legacy code that cannot otherwise be made working.

On the other hand, your architecture is broken. You shouldn't write a class that "magically" knows about the configuration just by accessing a variable that is supposed to be initialized somewhere else. A good pattern would be to pass the configuration as a parameter to the constructor:

public function __construct ($routes)
{
    $this->routes = $routes;
}

The part of the code that creates this class is supposed to grab the configuration from somewhere and pass it as a parameter. This concept is called inversion of control or dependency injection: Classes do not invoke the other classes they need to work with, they ask for them and get them as a parameter.

这篇关于Composer 自动加载文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆