Myconfig/service.yaml
contains several services that have huge configuration. Now, I want to move the configuration of that services to a separate file.
I tried to do like this:
At the end ofservice.yaml
:
imports:
- { resource: 'packages/custom_service.yaml' }
config/packages/custom_service.yaml
:
services:
App\Service\CustomService:
arguments:
$forms:
- location: 'room1'
id: 43543
- location: 'room2'
id: 6476546
- location: 'room3'
id: 121231
...
src/Service/CustomService.php
:
/**
* @var array
*/
protected $forms;
public function __construct(array $forms)
{
$this->forms = $forms;
}
But when I try to autowire in some Controller, I am getting this error:
Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" is type-hinted "array", you should configure its value explicitly.
But if I remove type hint, Then i get this error:
Cannot resolve argument $customService of "App\Controller\CustomController::customAction()": Cannot autowire service "App\Service\CustomService": argument "$forms" of method "__construct()" has no type-hint, you should configure its value explicitly.
The other two answers are partially correct but they don't quite cover everything.
To start with, anytime you decide you want multiple autowired service files you have to be careful that a given service is only autowired by one and only one file. Otherwise Symfony will attempt to define the service multiple times and usually fail. There are different approaches to accomplishing this but the most straight forward one is to explicitly exclude any custom services your config/services.yaml file:
# config/services.yaml
parameters:
imports:
- { resource: 'services/custom_service.yaml' }
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.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/Service/CustomService.php' # ADD THIS
- '../src/DependencyInjection/'
- # the rest of the default excludes
So that takes care of skipping CustomService.php in the main config file.
Secondly, notice in the above file I loaded the custom_service.yaml file from config/services instead of config/packages. The packages is really reserved for bundle specific configuration. Every file in there is loaded by default. Putting service files in there may or may not work but it definitely changes the loading order of things and could cause problems. So make a directory just for custom services and use it.
You also need to repeat the _defaults section in each service file since _defaults are considered to be file specific. Bit of a pain perhaps but that is the way it is:
# config/services/custom_service.yaml
services:
_defaults:
autowire: true
autoconfigure: true
App\Service\CustomService:
arguments:
$forms:
-
location: 'room1'
id: 43543
-
location: 'room2'
id: 6476546
-
location: 'room3'
id: 121231
Also note that your yaml array syntax was messed up. Possibly just a copy/paste issue.
And that should do it.
Autowiring has file scope.
You need to set it inconfig/packages/custom_service.yaml
as well.
Modify that file as follows
services:
_defaults:
autowire: true
App\Service\CustomService:
[...]
You should also addautoconfigure
and/or visibility (public
keyword) if needed.
Unfortunately, I cannot add a comment so I have to use an answer.
I have to make the following assumption, I believe you have the following snippet inside yourservices.yaml
:
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
which already includes your desired classApp\Service\CustomService
and so Symfony will try to autowire it before looking inside theimports
which leads to the error because you have that class defined inside theimport
which will only be evaluated if the rootservices.yaml
does not already find the class.
The following example will work. However, you will lose auto-discovery of classes inside theApp
namespace and it is not recommended to do this.
services.yaml
:
parameters:
imports:
- { resource: 'custom_services.yaml' }
services:
# default configuration for services in *this* file
_defaults:
autowire: true
autoconfigure: true
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
custom_services.yaml
:
services:
_defaults:
autowire: true
autoconfigure: true
App\Service\SomeService:
arguments:
$forms:
- location: 'room1'
id: 43543
- location: 'room2'
id: 6476546
- location: 'room3'
id: 121231
src/Service/SomeService.php
:
<?php
declare(strict_types=1);
namespace App\Service;
class SomeService
{
protected array $forms;
public function __construct(array $forms)
{
$this->forms = $forms;
}
public function getForms()
{
return $this->forms;
}
}
src/Controller/HomeController
:
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Service\SomeService;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class HomeController
{
#[Route('/', 'home')]
public function __invoke(SomeService $someService)
{
return new JsonResponse($someService->getForms());
}
}
What you can do instead is import the file in the Kernel instead in the yaml file which will overwrite the service definition.
So remove the imports part inside yourservices.yaml
and make sure that you import the file inside thesrc/Kernel.php
:
protected function configureContainer(ContainerConfigurator $container): void
{
$container->import('../config/{packages}/*.yaml');
$container->import('../config/{packages}/'.$this->environment.'/*.yaml');
if (is_file(\dirname(__DIR__).'/config/services.yaml')) {
$container->import('../config/services.yaml');
// Import your custom service files after services.yaml to overwrite already existing definitions
$container->import('../config/custom_services.yaml');
$container->import('../config/{services}_'.$this->environment.'.yaml');
} elseif (is_file($path = \dirname(__DIR__).'/config/services.php')) {
(require $path)($container->withPath($path), $this);
}
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Symfony compares favorably with other PHP frameworks in terms of reliability and maturity. This framework appeared a long time ago, in 2005, that is, it has existed much longer than most of the other tools we are considering. It is popular for its web standards compliance and PHP design patterns.
https://symfony.com/
Foundation, similar to Bootstrap, has become very popular as a more complex framework with some advanced but easy-to-implement CSS components. It is built with Sass, so just like Bootstrap, it is customizable. In addition to this, it also boasts some features that help make the design mobile responsive.
https://get.foundation/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.