Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected to find class X in file while importing services from resource "../src/", but it was not found after structural changes

In Symfony 4 I get:

Expected to find class "App\Controller\Quote\QuoteController" 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").

I had following structure:

app 
├── bin
├── config
├── src
│   ├── Controller
│   |   ├── QuoteController.php
│   ├── Entity
│   |   ├── Quote.php
│   ├── Repository
│   |   ├── QuoteRepository.php

with for example QuoteControler being:

<?php

namespace App\Controller;

use App\Repository\QuoteRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

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(),
        ]);
    }
}

And everything worked fine. I now changed my directory structure to this:

app 
├── bin
├── config
├── src
│   ├── Controller
│   |   ├── Quote
│   |   |   ├── QuoteController.php
│   ├── Entity
│   |   ├── Quote
│   |   |   ├── Quote.php
│   ├── Repository
│   |   ├── Quote
│   |   |   ├── QuoteRepository.php

And I got this error after deleting the contents of var/cache. I even changed all use App\Repository\QuoteRepository; to use App\Repository\Quote\QuoteRepository; (and the similar statements in Quote.php and QuoteRepository) which didn't work.

What do I need to change to make it work?

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/"
        }
    },
...

Could someone please help me, this is my first symfony project and for my limited understanding at this moment it should have something to do with the autoloaded namespace and/or the services declaration but I just couldn't find the values I need to provide.

like image 656
tier2developer Avatar asked Nov 01 '25 11:11

tier2developer


1 Answers

  1. make sure you updated namespace from "namespace App\Controller;" to namespace App\Controller\Quote;
  2. run "composer dump-autoload" to be sure that composer is not the issue
like image 64
fico7489 Avatar answered Nov 04 '25 01:11

fico7489



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!