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

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

问题描述

我的自动载入规格如下

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

routes.php文件的内容是:

 <?php 
使用MyMVC \Core\Route;

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


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

 注意:未定义的变量:route in /var/www/html/mymvc/app/init.php on line 29 


$ b b

这里是我试图使用 $ route 对象。

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



我也跑了命令 composer dump-autoload

解决方案

自动加载在这里无效。 PHP只能自动加载类。您期望 app / routes.php 将无法自动加载,因为该文件不包含类声明,并且您无法使用



当你包含 vendor / autoload.php 时,Composer会执行该文件一次, - 但是,这是真正的坏行为的软件。不要使用文件自动加载包括配置文件。注意在图书馆中使用时可能产生的性能影响。你应该避免使用它,它是为了用于旧的代码,否则不能工作。



另一方面,你的架构被打破了。你不应该写一个类,magically知道配置只是通过访问一个变量,应该在其他地方初始化。一个好的模式是将配置作为参数传递给构造函数:

  public function __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 MyMVC\Core\Route;

$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天全站免登陆