PSR4 Composer自动加载名称空间 [英] PSR4 Composer Autoloading namespaces

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

问题描述

我一直在尝试一些Composer自动加载,并且遇到了一些问题,因此目录结构为

I've been having a little play around with some Composer autoloading and i'm getting some issues so the directory structure is

index.php
app/
   helpers/
          router.php
vendor/
   composer/
          /*usual files*/
   autoload.php

我的composer.json内有以下内容

"autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }

我的index.php内部有

<?php
// Autoload our namespaces
require __DIR__.'/vendor/autoload.php';

use App\Helpers\Router;
$route = new Router;

出现以下错误

Fatal error: Class 'App\Helpers\Router' not found in /var/www/public/index.php on line 6

我尝试了几种不同的尝试来使其正常运行,但是我不确定我要去哪里.这是我第一次尝试在框架外部使用Composer进行自动加载,因此不胜感激.

I have tried a few different things to try and get it working but i'm unsure where i'm going wrong. This is my first time looking into autoloading using Composer outside of a framework so would appreciate any guidance.

推荐答案

PSR-4区分大小写.该结构必须为app/Helpers/Router.php或带有大写字母A的更好App.

PSR-4 is case sensitive. The structure has to be app/Helpers/Router.php or better App with capital A.

必须以区分大小写的方式引用所有类名.

All class names MUST be referenced in a case-sensitive fashion.

子目录名称必须与子命名空间名称的大小写匹配.

The subdirectory name MUST match the case of the sub-namespace names.

终止类名对应于以.php结尾的文件名.文件名必须与终止类名的大小写匹配.

The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.

http://www.php-fig.org/psr/psr-4/

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

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