简体   繁体   English

Composer PSR-0自动加载不起作用

[英]Composer PSR-0 autoloading dont work

I have problem with autoloading module controller class from my app. 我的应用程序自动加载模块控制器类有问题。 I try to configure composer.json but still not working. 我尝试配置composer.json但仍然无法正常工作。 Probably am doing somthing wrong with this directory structure. 可能我正在做这个目录结构的错误。 I try examples from composer doc but again dont work... 我尝试了作曲家doc的例子,但又一次不工作......

Directory structure: 目录结构:

|- admin
|----- modules
|--------- Menu
|------------Controller
|--------------MenuController.php

Lets see composer.json 让我们看看composer.json

{
  "autoload": {
    "psr-0": { "Admin\\Modules\\": "" }
  }
}

I try to set path but nothing again // "Admin\\Modules\\": "admin/modules" 我尝试设置路径,但没有再次//“Admin \\ Modules \\”:“admin / modules”

Menu controller : 菜单控制器

namespace Admin\Modules\Menu\Controller;

class MenuController extends AbstractAdminBaseController

FrontController FrontController

require 'vendor/autoload.php';

new \Admin\Modules\Menu\Controller\MenuController();

All time Class not found. 所有时间都没有找到。 I try 100 examples from google and nothing. 我尝试了谷歌的100个例子,没有。 Any example how to slowe this problem? 如何减缓这个问题的任何例子? Thanks 谢谢

Update structure: 更新结构:

├───admin
│   └───modules
│         └───Menu
│            └───controller
│                    └───MenuController.php
├───vednor
│   └───autoload.php
│   └───composer
│          └───autoload_classmap.php
│          └───autoload_namespaces.php
│          └───autoload_psr4.php
│          └───autoload_real.php
│          └───ClassLoader.php
├───public
├───assets
├───index.php
├───composer.json
├───composer.lock

Your PSR-0 will never work, because this standard dictates that the path to the file has to be EXACTLY like the classname. 你的PSR-0永远不会工作,因为这个标准规定文件的路径必须与类名完全一样。 Note that your first part of the namespace is "Admin", but the first directory part is only "admin" - cases must match exactly, or it won't work (or will only work with case insensitive filesystems). 请注意,命名空间的第一部分是“Admin”,但第一个目录部分只是“admin” - 案例必须完全匹配,否则它将不起作用(或仅适用于不区分大小写的文件系统)。

You will succeed with using PSR-4, though. 但是,您将成功使用PSR-4。 Why? 为什么? Because with PSR-4, the given namespace prefix is removed from the full classname, and the rest is being converted into a path that is searched in the directory given for the namespace prefix. 因为使用PSR-4,将从完整的类名中删除给定的名称空间前缀,其余的将被转换为在为名称空间前缀指定的目录中搜索的路径。

Example for your case: 案例示例:

"autoload": {
  "psr-0": { "Admin\\Modules\\": "" }
}

Will not work because the files are in path admin/modules , but must be in Admin/Modules . 无法工作,因为文件位于路径admin/modules ,但必须位于Admin/Modules

"autoload": {
  "psr-4": { "Admin\\Modules\\": "admin/modules/" }
}

Will work because the prefix Admin\\Modules\\ is removed and the remaining class name is being converted to a path and added to admin/modules . 将起作用,因为删除了前缀Admin\\Modules\\ ,并将剩余的类名转换为路径并添加到admin/modules

Ah, one gotcha! 啊,有一个问题! It will NOT work, because you chose to name the class ...\\Controller\\... , but the path once again .../controller/... . 它不起作用,因为你选择命名类...\\Controller\\... ,但路径再次.../controller/...

Honestly, I'd highly recommend to convert your file names and location to PSR-4 compatibility, even for the prefixed directories that you'd be able to work around with Composer. 老实说,我强烈建议将您的文件名和位置转换为PSR-4兼容性,即使对于您可以使用Composer解决的前缀目录也是如此。 This will eliminate the surprising lowercase directory structures I see. 这将消除我看到的令人惊讶的小写目录结构。

I mean: Why is that controller directory even lower case in the first place if every class located there is Controller ? 我的意思是:如果位于那里的每个类都是Controller那么为什么controller目录首先是小写? I really can't understand this. 我真的无法理解这一点。

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

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