在Symfony 4中我得到:
Expected to find class "AppControllerQuoteQuoteController" in file "/var/www/MyApp/src/Controller/Quote/QuoteController.php" while importing services from resource "../src/", but it was not found! Check the namespace prefix used with the resource in /var/www/MyApp/config/services.yaml (which is being imported from "/var/www/MyApp/src/Kernel.php").
我有如下结构:
app
├── bin
├── config
├── src
│ ├── Controller
│ | ├── QuoteController.php
│ ├── Entity
│ | ├── Quote.php
│ ├── Repository
│ | ├── QuoteRepository.php
,例如quoteconcontroller为:
<?php
namespace AppController;
use AppRepositoryQuoteRepository;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
class QuoteController extends AbstractController
{
#[Route('/quote', name: 'app_quote')]
public function index(QuoteRepository $quoteRepository)
: Response {
return $this->render('quote/index.html.twig', [
'controller_name' => 'QuoteController',
'quotes' => $quoteRepository->findAll(),
]);
}
}
一切都很好。现在我把目录结构改成这样:
app
├── bin
├── config
├── src
│ ├── Controller
│ | ├── Quote
│ | | ├── QuoteController.php
│ ├── Entity
│ | ├── Quote
│ | | ├── Quote.php
│ ├── Repository
│ | ├── Quote
│ | | ├── QuoteRepository.php
在删除var/cache的内容后,我得到了这个错误。我甚至将所有的use AppRepositoryQuoteRepository;
更改为use AppRepositoryQuoteQuoteRepository;
(以及Quote.php和quoterrepository中的类似语句),但都不起作用。
我需要改变什么来使它工作?
services.yaml:
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
App:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
composer.json:
...
"autoload": {
"psr-4": {
"App\": "src/"
}
},
...
有人可以帮助我,这是我的第一个symfony项目和我有限的理解在这一刻,它应该与自动加载的命名空间和/或服务声明有关,但我只是找不到我需要提供的值。
始终检查引用文件中的名称空间是否正确,如Bossman的注释所述:
将命名空间从
namespace AppController;
更新为namespace AppControllerQuote;