I've started a Symfony project for an API and have created my first controller
# services.yaml
parameters:
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
App\Controller\:
resource: '../src/EndPoints/*'
tags: ['controller.service_arguments']
Controller:
namespace App\Controller;
class RegisterController extends AbstractController {
public function register(Request $request): Response {
//stuff
}
}
I run this the first time and get an expected result. I do modifications to RegisterController
and it dies with the error
Compile Error: Cannot declare class App\Controller\RegisterController, because the name is already in use
If I go into the services.yaml
and save it (no modifications) I can run it again with the updated code.
This has only just started happening when I've added doctrine-test-bundle
and been doing testing however I don't think the two things are related. I've checked my .env.local
APP_ENV
is dev. What is causing a cache that means I have to resave services.yaml
for any change to work?
You are importing the controllers twice.
Here, you are importing all your classes, but your controllers are not excluded from auto-wiring:
And here you import your controller classes again
When the container gets compiled, you end up with a double definition for these classes.
Just do:
and you'll be set.
Additionally, you have a PSR-4 mismatch, where the classes at
src/Endpoints
belong to the namespaceController
. That's not problematic per-se, but it will only work if yourcomposer.json
is properly set. Better have directories that match the namespaces, do not make it harder than it should be.