我已经检查了类似的问题但是由其他人发布但我仍然看不出我的代码中有什么问题。
我刚刚从文档中复制了它 - https://symfony.com/doc/3.4/page_creation.html
<?php
// src/AppBundle/Controller/LuckyController.php
namespace AppBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class LuckyController
{
/**
* @Route("/lucky/number")
*/
public function numberAction()
{
$number = mt_rand(0, 100);
return new Response(
'<html><body>Lucky number: '.$number.'</body></html>'
);
}
}
自动加载器期望类“App \ Controller \ LuckyController”在文件“E:\ projektai \ php projektai \ htdocs \ mokomieji \ weather_demo_3 \ vendor \ composer /../../ src \ Controller \ LuckyController.php”中定义。找到该文件但该类不在其中,类名或命名空间可能在E:\ projektai \ php projektai \ htdocs \ mokomieji \ weather_demo_3 \ config / services.yaml中有一个拼写错误(在资源“E:中加载) \ projektai \ php projektai \ htdocs \ mokomieji \ weather_demo_3 \ config / services.yaml“)。
所以它说文件被找到但是找不到类。如何找不到课程?我看到了。它是如何尝试搜索它的?
composer.json
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.0.8",
"symfony/console": "^3.4",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^3.4",
"symfony/lts": "^3",
"symfony/yaml": "^3.4"
},
"require-dev": {
"symfony/dotenv": "^3.4"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"id": "01C16444CCZTY1MPPEE5GJZ63G",
"allow-contrib": false
}
}
}
基于该结构,您可能正在使用Symfony Flex,因此您的命名空间应该是namespace App\Controller;
在我的情况下,我有一段时间没有使用过PHP,我也从教程中复制/粘贴,但实际上我忘了添加
<?php
在文件的开头。导致相同的错误消息。