[英]issues when autoloading files with psr-4
I've been trying to get psr-4 autoloading work for over a week now with no success. 我一直在努力使psr-4自动加载工作超过一个星期,但没有成功。
My file structure is as follows: 我的文件结构如下:
-Project
-src
-classes
session.php
-vendor
index.php
I've created the psr-4 autoload function as follows: 我创建了psr-4自动加载函数,如下所示:
"autoload": {
"psr-4": {
"classes\\": "src/classes"
}
}
after using composer dump-autoload -0 ,inside my session.php class I gave the namespace: 在使用composer dump-autoload -0之后,在session.php类中,我指定了名称空间:
namespace classes;
class session{
public static function exist($name){
return(isset($_SESSION[$name])) ? true : false;
}
I then required the autoloader and used the use function to name the session class as follows: 然后,我需要自动加载器,并使用use函数来命名会话类,如下所示:
use src\classes\session as session;
require_once('vendor/autoload.php');
session::put('test', 'test');
after opening up the index.php page, I get a 打开index.php页面后,我得到一个
Fatal error: Class 'src\\classes\\session' not found in /var/www/test/Project/index.php on line 10 致命错误:在第10行的/var/www/test/Project/index.php中找不到类'src \\ classes \\ session'
is my directory structure / php correct? 我的目录结构/ php正确吗? I've tried many different guides online and can't seem to get it to work. 我在网上尝试了许多不同的指南,但似乎无法使其正常工作。
Most simple solution: 最简单的解决方案:
use classes\session;
require_once('vendor/autoload.php');
session::put('test', 'test');
However, you probably don't want to use classes
as a vendor namespace, but instead adjust a few things here and there: 但是,您可能不想将classes
用作供应商名称空间,而是在此处和此处进行一些调整:
-Project
-src
Session.php
-public
index.php
-vendor
composer.json
自动加载composer.json
配置 {
"autoload": {
"psr-4": {
"Juakali\\": "src"
}
}
}
Replace Juakali
with a vendor namespace you prefer, this is just a suggestion. 用您喜欢的供应商名称空间替换Juakali
,这只是一个建议。 Ideally, if you intend to publish your package, it should be one that isn't already claimed by someone else, see https://packagist.org . 理想情况下,如果您打算发布您的软件包,则应该是尚未有人声明过的软件包,请参阅https://packagist.org 。
For reference, see 供参考,请参阅
Juakali\\Session
Use the aforementioned vendor namespace of your choice: 使用您选择的上述供应商名称空间:
namespace Juakali;
class Session
{
public static function exist($name)
{
return isset($_SESSION[$name]);
}
}
Consider using a widely used coding style, for example PSR-2. 考虑使用广泛使用的编码样式,例如PSR-2。
For reference, see 供参考,请参阅
index.php
Assuming that you want to expose index.php
as the entry point for a web application, move it into a directory which you feel confident to expose as a document root of your web server, adjust the import in index.php
, as well as the path to vendor/autoload.php
: 假设您希望将index.php
作为Web应用程序的入口点公开,请将其移至您有信心作为Web服务器的文档根目录公开的目录中,调整index.php
的导入以及vendor/autoload.php
路径:
use Juakali\Session;
require_once __DIR__ . '/../vendor/autoload.php';
Session::put('test', 'test');
It looks like you're defining your alias of "src/classes" as 'classes'. 看起来您正在将“ src / classes”的别名定义为“ classes”。 So you need to use: 因此,您需要使用:
use classes\session;
Instead 代替
More info: PSR-4 autoloader Fatal error: Class not found 更多信息: PSR-4自动装带器致命错误:找不到类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.