Symfony 2 InvalidArgumentException when creating a new service - php

I'm trying to create my first service in a symfony 2 application and I get this error :
InvalidArgumentException: There is no extension able to load the
configuration for "my_app.myservice" (in
/path/to/src/MyApp/MyBundle/DependencyInjection/../Resources/config/services.yml).
Looked for namespace "my_app.myservice", found none.
It seems there's a problem in my configuration but I don't see what it is.
Here's my services.yml
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService
And my service looks like this
<?php
namespace MyApp\MyBundle\Service;
class MyService
{
public function run()
{
echo "hello world";
}
}
Thanks for help !

Just to be sure - do you have proper indentation in the services.yml?
It should be:
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService
not:
services:
my_app.myservice:
class: MyApp\MyBundle\Service\MyService

Do you have any argument in your service's constructor ?
For example i pass the logger in all my services.
it's result that i have to pass it in my service.yml
service.yml
services:
blog:
class:Site\Backend\BlogBundle\Service\BlogService
arguments: [#logger]
tags:
- { name: monolog.logger, channel: blog}
BlogService
public function __construct(LoggerInterface $logger){
$this->logger = $logger;
}

Related

Controller has no container set, did you forget to define it as a service subscriber? Symfony 5

I'm making my first application on Symfony 5
I ran into a problem when creating a controller, and I need a controller, not a service
Uncaught PHP Exception LogicException: ""App\Controller\UploadFileController" has no container set, did you forget to define it as a service subscriber?" at /vendor/symfony/framework-bundle/Controller/ControllerResolver.php line 36
it is my simple Controller
class UploadFileController extends AbstractController
{
/**
* #Route("/test-upload", name="app_test_upload")
*/
public function testAction(Request $request, FileUploader $fileUploader)
{
return 1;
}
}
I tried to clean the cache, it didn't help
this is my services.yaml
services:
App\Controller\UploadFileController:
calls:
- [setContainer, ['#service_container']]
There is no need to add any releated information in your service.yaml file, you only need to edit your code in service.yaml file to be like that:
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']

Adding services to a Controller through "container.service_subscriber" not working as expected

I am trying to use the container.service_subscriber tag on my Controller to make some services available without injecting them through the constructor. In our project we don't want to use the autowiring and also can't use the autoconfigure option.
The structure of the Controller is as follow:
I have a base BaseController which extends from the AbstractFOSRestController of FOSRestBundle which has some common used methods for all my Controllers. That service will be used as parent for my other Controllers.
The service definition looks like this:
WM\ApiBundle\Controller\BaseController:
class: WM\ApiBundle\Controller\BaseController
abstract: true
arguments:
- "#service1"
- "#service2"
- ...
WM\ApiBundle\Controller\UserController:
parent: WM\ApiBundle\Controller\BaseController
public: true
#autowire: true
class: WM\ApiBundle\Controller\UserController
tags:
- { name: 'container.service_subscriber'}
- { name: 'container.service_subscriber', key: 'servicexyz', id: 'servicexyz' }
The class looks like this:
/**
* User controller.
*/
class UserController extends AbstractCRUDController implements ClassResourceInterface
{
public static function getSubscribedServices()
{
return array_merge(parent::getSubscribedServices(), [
'servicexyz' => ServiceXYZ::class,
]);
}
.......
}
The problem I have is, if I set autowire: false, it always automatically sets the full container and with this the appropriate deprecation message (as I am not setting it myself):
User Deprecated: Auto-injection of the container for "WM\ApiBundle\Controller\UserController" is deprecated since Symfony 4.2. Configure it as a service instead.
When setting autowire: true Symfony does respect the container.service_subscriber tag and only sets the partial container (ServiceLocator), which also would solve the deprecation message. I would have expected that autowiring should not make any differences in this case because I am explicitly telling the service which other services it should have.
Am I using the tags wrong or do I have a general problem in understanding how to subscribe a service to a Controller?
The basic issue is that the builtin service subscriber functionality will only inject the service locator into the constructor. A conventional controller which extends AbstractController uses autoconfigure to basically override this and uses setContainer instead of the constructor.
# ApiBundle/Resources/config/services.yaml
services:
_defaults:
autowire: false
autoconfigure: false
Api\Controller\UserController:
public: true
tags: ['container.service_subscriber']
class UserController extends AbstractController
{
protected $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public static function getSubscribedServices()
{
return array_merge(parent::getSubscribedServices(), [
// ...
'logger' => LoggerInterface::class,
]);
}
public function index()
{
$url = $this->generateUrl('user'); // Works as expected
// $signer = $this->get('uri_signer'); // Fails as expected
$logger = $this->get('logger'); // Works as expected
return new Response('API Index Controller ' . get_class($this->container));
}
}
Results in:
API Index Controller Symfony\Component\DependencyInjection\Argument\ServiceLocator
Indicating that a service locator (as opposed to the global container is being injected).
You can also configure your service to use the setContainer method and eliminate the need for a constructor. Either approach will work.
Api\Controller\UserController:
public: true
tags: ['container.service_subscriber']
calls: [['setContainer', ['#Psr\Container\ContainerInterface']]]
Solution to the problem is to extend the service definition of the Controller with a call to setContainer to inject the '#Psr\Container\ContainerInterface' service:
WM\ApiBundle\Controller\BaseController:
class: WM\ApiBundle\Controller\BaseController
abstract: true
arguments:
- "#service1"
- "#service2"
- ...
calls:
- ['setContainer', ['#Psr\Container\ContainerInterface']]
WM\ApiBundle\Controller\UserController:
parent: WM\ApiBundle\Controller\BaseController
public: true
class: WM\ApiBundle\Controller\UserController
tags:
- { name: 'container.service_subscriber'}
- { name: 'container.service_subscriber', key: 'servicexyz', id: 'servicexyz' }
This will give me a ServiceLocator as container containing only the regiestered services instead of the full container without using the autowire option.
Sidenote: Setting the #service_container would inject the full container.
For completeness, there was already an issue on the symfony project where this was discussed.

Symfony4: inject service into controller __construct (constructor)

I am having trouble accessing an injected service in the constructor of one of my controllers.
Per http://symfony.com/doc/current/service_container/injection_types.html I believe I have done the injection correctly, however when I try to load a view from the controller, I get the following error:
Argument 1 passed to Regions\AnalyticsBundle\Controller\PatternsController::__construct()
must be an instance of Regions\AnalyticsBundle\Controller\PatternCacheService, instance of
Regions\AnalyticsBundle\Service\PatternCacheService given, called
in /var/tmp/symfony/cache/dev/ContainerLoHUcSH/getPatternsControllerService.php on line 9
It seems like the error indicates that the type hinting in the constructor is trying making it look for an instance in the *\Controller\* namespace instead of the *\Services\* namespace - what am I doing wrong or not seeing here?
Details of my setup are as follows...
Symfony 4.1.0, PHP 7.2.5
services.yaml
services:
...
pattern_cache_service:
class: Regions\AnalyticsBundle\Service\PatternCacheService
public: true
Regions\AnalyticsBundle\Controller\PatternsController:
arguments: ['#pattern_cache_service']
Controller:
namespace Regions\AnalyticsBundle\Controller;
class PatternsController extends BaseController
{
private $pcs;
public function __construct(PatternCacheService $pcs)
{
$this->pcs = $pcs;
}
}
You forgot a use in your Controller, making PHP think that your service is in the same namespace as your controller.
<?php
namespace Regions\AnalyticsBundle\Controller;
use Regions\AnalyticsBundle\Service\PatternCacheService;
class PatternsController extends BaseController
{
private $pcs;
public function __construct(PatternCacheService $pcs)
{
$this->pcs = $pcs;
}
}
This was actually raised as part of your error message
Argument 1 passed to Regions\AnalyticsBundle\Controller\PatternsController::__construct()
must be an instance of Regions\AnalyticsBundle\Controller\PatternCacheService
When what you expected was your controller to need an instance of Regions\AnalyticsBundle\Service\PatternCacheService
The class PatternCacheService cannot be found in the namespace Regions\AnalyticsBundle\Controller.
Add an import:
<?php
namespace Regions\AnalyticsBundle\Controller;
use Regions\AnalyticsBundle\Service\PatternCacheService;
class PatternsController extends BaseController
{
private $pcs;
public function __construct(PatternCacheService $pcs)
{
$this->pcs = $pcs;
}
}
For reference, see
http://php.net/manual/en/language.namespaces.importing.php
You don't need a service definition for pattern_cache_service. It should autowire your service if autowire: true is set.
PatternCacheService should be private as you don't want to access it from within container. Suggested practise!
You don't need a service definition for PatternsController either.
Note: You should not use "bundles" anymore in Symfony 4 so I would get rid of AnalyticsBundle.
Note: Better organise your configuration files as show here: Organising route, service and parameter configuration files in symfony 4 applications.
This should suffice:
services.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/*'
exclude: '../src/{Entity,....so on .....,Kernel.php}'
App\Controller\:
resource: '../../src/Regions/AnalyticsBundle/Controller'
tags: ['controller.service_arguments']
PatternsController
namespace Regions\AnalyticsBundle\Controller;
use Regions\AnalyticsBundle\Service\PatternCacheService;
class PatternsController
{
private $pcs;
public function __construct(PatternCacheService $pcs)
{
$this->pcs = $pcs;
}
}

Symfony add doctrine to services.yml and custom extensions

What I want achieve is call doctrine ini TwigExtension, I get some code from google and stackoverflow, like this :
service.yml
twig.extension:
class: AppBundle\Twig\AppExtension
arguments:
doctrine : '#doctrine'
tags:
- { name: twig.extension }
AppBundle\Twig\AppEtension.php
<?php
namespace AppBundle\Twig;
use Symfony\Bridge\Doctrine\RegistryInterface;
class AppExtension extends \Twig_Extension
{
protected $doctrine;
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
}
I've followed all instruction from my source, but i still got error and cant call doctrine or do something with entitymanager here.
this is my error :
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
Invalid key "doctrine" found in arguments of method "__construct()" for service "twig.extension": only integer or $named arguments are allowed.
how to solve this?
In service.yml, instead of doctrine : '#doctrine' you should have $doctrine : '#doctrine'.
The error you get makes an attempt at telling you this by saying only integer or $named arguments are allowed - note the **$**named part.

Dependency Injection in Symfony

I'm building Symfony 3.3 application. I have a helper in a Console folder:
abstract class AbstractHelper implements HelperInterface
{
protected $httpClient;
public function __construct(HttpInterface $httpClient)
{
$this->httpClient = $httpClient;
}
}
And I have implementation of HttpInterface named HttpGuzzle into Service folder. How could I help Symfony to figure out I want to inject HttpGuzzle into AbstractHelper constructor? I tried to add these line to services.yml but it doesn't work:
AppBundle\Command\AbstractHelper:
arguments:
$httpClient: AppBundle\Service\HttpGuzzle
If i run the tests it throws an error:
ArgumentCountError: Too few arguments to function AppBundle\Command\AbstractHelper::__construct(),
0 passed in ~/Projects/app/tests/AppBundle/Console/HelperTest.php
on line 17 and exactly 1 expected
With this:
helper:
class: AppBundle\Command\AbstractHelper:
arguments: [AppBundle\Service\HttpGuzzle]
I get an error:
You have requested a non-existent service "helper".
In services.yml You have to define the HttpGuzzle service itself, like this:
httpguzzle:
class: AppBundle\Service\HttpGuzzle
Then you can use pass it to the helper like this:
helper:
class: AppBundle\Command\AbstractHelper
arguments: ["#httpguzzle"]

Categories