8

I'm trying to add an old Bundle that I have built on Symfony 3.* to Symfony 4 but I get this error:

The autoloader expected class "App\SBC\TiersBundle\Controller\ChantierController" to be defined in file "/Applications/MAMP/htdocs/Projects/HelloSymfony4/vendor/composer/../../src/SBC/TiersBundle/Controller/ChantierController.php". The file was found but the class was not in it, the class name or namespace probably has a typo in /Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml (which is loaded in resource "/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml").

It seems like the framework did not recognise the namespace of the bundle so I did these steps:
In config/bundle.php I added the third line:

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    \SBC\TiersBundle\TiersBundle::class => ['all' => true], // this one
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
];

And in composer.json I added the first line in autoload section:

"autoload": {
        "psr-4": {
            "SBC\\": "src/SBC/",
            "App\\": "src/"

        }
    },

Because the namespace of my Bundle starts with SBC\, and I have launched composer dump-autoload in the console.

<?php

namespace SBC\TiersBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class TiersBundle extends Bundle
{
}

ChantierController.php:

namespace SBC\TiersBundle\Controller;

use SBC\TiersBundle\Entity\Chantier;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;


class ChantierController extends Controller
{
...
}

And this is my Bundle under /src:
enter image description here

Unfortunately still facing the same error, how can I fix it and thanks in advance.

UPDATE: config/services.yaml:

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    SBC\:
        resource: '../src/SBC/*'
        exclude: '../src/SBC/TiersBundle/{Entity,Migrations,Tests}'

    SBC\TiersBundle\Controller\:
        resource: '../src/SBC/TiersBundle/Controller'
        tags: ['controller.service_arguments']

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
SlimenTN
  • 3,383
  • 7
  • 31
  • 78

1 Answers1

7

The problem is most likely caused by Symfony configuration and conflict in namespaces. First you need to adjust your config/services.yaml:

SBC\:
    resource: '../src/SBC/*'
    exclude: '../src/SBC/TiersBundle/{Entity,Migrations,Tests,Kernel.php}'

SBC\TiersBundle\Controller\:
    resource: '../src/SBC/TiersBundle/Controller'
    tags: ['controller.service_arguments']

App\:
    resource: '../src/*'
    exclude: '../src/{SBC,Entity,Migrations,Tests,Kernel.php}'

This way you'll define defaults for your namespace and prevent the default namespace App to include your directory when generating autoload classes. Note that if you are using annotation routes, you also need to adjust config/routes/annotations.yaml:

sbc_controllers:
    resource: ../../src/SBC/TiersBundle/Controller/
    type: annotation

so the routes are generated correctly. After performing these steps run composer dump-autoload again and clear Symfony's cache.

This might be helpful in the future if you run into another problems: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md

Jakub Krawczyk
  • 950
  • 8
  • 16
  • Thanks for your answer I will try it and give you feedback, and for the type I'm using `yaml` for routes not `annotation` so I should change the type to yaml ? – SlimenTN Feb 19 '18 at 09:51
  • If you are using `yaml` for defining your routes it shouldn't be necessary to change anything. – Jakub Krawczyk Feb 19 '18 at 09:53
  • After changing services.yaml I get this exception: `Invalid "exclude" pattern when importing classes for "SBC\": make sure your "exclude" pattern (../src/{Entity,Migrations,Tests,Kernel.php}) is a subset of the "resource" pattern (../src/SBC/*) in /Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml (which is loaded in resource "/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml").`` – SlimenTN Feb 19 '18 at 09:56
  • Sorry, I made a mistake & updated my answer. I believe you need to exclude `../src/SBC/TiersBundle/{Entity,Migrations,Tests,Kernel.php}` for the `SBC\` namespace. – Jakub Krawczyk Feb 19 '18 at 09:59
  • Now a new error : Class Symfony\Component\Form\AbstractType not found in /Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml (which is loaded in resource "/Applications/MAMP/htdocs/Projects/HelloSymfony4/config/services.yaml"). – SlimenTN Feb 19 '18 at 10:05
  • That's another problem - I would need to see your services.yaml. – Jakub Krawczyk Feb 19 '18 at 10:06
  • sorry for my late answer I have updated my question. – SlimenTN Feb 19 '18 at 10:20
  • Do you have form component installed? Symfony 4 differs from previous versions, skeleton version keeps only the bare minimum. You need to run `composer require form` first, because your form element probably depends on a nonexistent class. – Jakub Krawczyk Feb 19 '18 at 10:30
  • you're right now the error is gone and another one rises XD, I'm sorry for wasting your time, this is the new exception: Compile Error: Cannot declare class SBC\TiersBundle\Controller\ChantierController, because the name is already in use – SlimenTN Feb 19 '18 at 10:45
  • Try to change the `exclude` section for `App\` namespace to: `exclude: '../src/{SBC/TiersBundle,Entity,Migrations,Tests,Kernel.php}'` - it is most likely colliding in some way. Also be sure that you run `composer dump-autoload` after these adjustments. – Jakub Krawczyk Feb 19 '18 at 10:48
  • sorry I can't find this `exclude` section in `services.yaml`! – SlimenTN Feb 19 '18 at 10:50
  • ah soryy I found it – SlimenTN Feb 19 '18 at 10:50
  • Very nice now it works fine and my Bundle is working, Thanks for your time I really appreciate it :) – SlimenTN Feb 19 '18 at 10:54