PHP-Composer自动加载-未找到类
我在通过composer使用自动加载的类时遇到问题 以下是我的文件结构:PHP-Composer自动加载-未找到类,php,Php,我在通过composer使用自动加载的类时遇到问题 以下是我的文件结构: -cache -public -src --Repository ---PostRepository.php -index.php -composer.json 我正在尝试在index.php中加载PostRepository.php。 但是有一个错误 致命错误:未捕获错误:类“App\Repository\PostRepository”不是 在/opt/lampp/htdocs/index.php中找到:12堆栈跟踪:
-cache
-public
-src
--Repository
---PostRepository.php
-index.php
-composer.json
我正在尝试在index.php中加载PostRepository.php。
但是有一个错误
致命错误:未捕获错误:类“App\Repository\PostRepository”不是
在/opt/lampp/htdocs/index.php中找到:12堆栈跟踪:#0{main}
在第12行的/opt/lampp/htdocs/index.php中
这是我的composer.json:
{
"require": {
"twig/twig": "^2.0"
},
"autoload": {
"psr-4": {
"App\\" : "src/"
}
}
}
这是我的PostRepository.php
<?php
namespace App\Repository;
class PostRepository
{
...
}
您应该在使用任何类之前包含autoload.php
请尝试使您的代码如下所示:
<?php
require __DIR__ . "/vendor/autoload.php";
use App\Repository\PostRepository;
$postClass = new PostRepository();
$posts = $postClass->getAllPosts();
var_dump($posts);
致命错误:未捕获错误:类“App\Repository\PostRepository”不是
在/opt/lampp/htdocs/index.php中找到:12堆栈跟踪:#0{main}
在第12行的/opt/lampp/htdocs/index.php中
htdocs/index.php!!您在htdocs中有一个项目吗?
因为您应该引用htdocs/your project folder/index.php
谢谢,但是我尝试了这个,但是得到了相同的错误。您是否引用了正确文件夹中的正确索引?哦,我的。。。您是对的,project位于我的本地主机上的错误目录中。。。感谢根据您的粘贴,错误发生在第12行。然而,postedindex.php只有9行。
<?php
require __DIR__ . "/vendor/autoload.php";
use App\Repository\PostRepository;
$postClass = new PostRepository();
$posts = $postClass->getAllPosts();
var_dump($posts);
composer dump-autoload