Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I switched from asp.net core and Laravel to Symfony5. And I have a problem with the my service and the configuration system.Dependency Injection still works even though I turned off autowiring. Also, the settings inside the dev folder do not override those in service.yaml!

What I have tried:

At the src\Services location, I created a simple php class as a simple service:

<pre lang="PHP">
<pre><?php
//src/Services

namespace App\Services;

class HelloService
{
    public function sayHello($name='John Doe')
    {
        return 'Hello ' . $name;
    }
}


and TestServiceController to test the service:

PHP
<pre><?php
//src/Controller
namespace App\Controller;

use App\Services\HelloService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class TestServiceController extends AbstractController
{
    #[Route('/test', name: 'test_service')]
    public function index(HelloService $hs): Response
    {
        return new Response($hs->sayHello());
    }
}


Based on Symfony documentation, since I created the service inside the src\ folder
my service should be automatically visible to the container and supports autowiring by default (services.yaml default conf.). When an application is launched in a dev environment everything works ok and I get it on the website:

Hello John Doe

However, now if I want to turn off autowiring, inside the service.yaml file I wrote:

Terminal
services:
....
.... rest configs
....
    App\Services\HelloService:
        autowire: false


and when I run command:

Terminal
php bin/console debug:container HelloService


i get confirmation that autowired no

However, the app still works and launches:

Hello John Doe

What bothers me is the following:
As I understood the documentation, an exception should be happened becose autowiring now is shutdown, automatic Dependency Injection should be disabled now and specifying the type of class-HelloService in the controller method - public function index (HelloService $ hs) it should be not work and application should throw an exception but it still works OK! Why? !!!

----
Also, overriding service settings does not work for me.
For example, if I have a setting in service.yaml:

Terminal
services:
....
.... rest configs
....
    App\Services\HelloService:
        autowire: true


and if I want to override default configs in services.yaml and turn off autowire in a dev environment,

Terminal
\config\packages\dev\HelloService.yaml :

services:
  App\Services\HelloService:
    autowire: false

when I run
Terminal
php bin/console debug: HelloService container


Im still get autowired: yes

Shouldn't the configurations defined within \packages\dev\<service>.yaml override those in service.yaml ?!

Also, the configurations defined in service_dev.yaml do not override those defined in service.yaml while the documentation claims otherwise or I misunderstood something.
Thank you very much in advance.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900