I'm learning Symfony2 at a new workplace and was tasked with adding validation to the code which is currently using poor inline based code instead of the Symfony validator and annotations.
I've added validation annotations to my model and added a parameter to my constructor. I've also added an argument to the service.yml so that it gets injected into my bundle's class, but it seems i cannot find the
#validator
as per described in the Symfony documentation (http://symfony.com/doc/current/book/validation.html). If i read the documentation right, i should be able to just add #validator to my services.yml and get it fed directly to my class but when running my tests, it says that the service #validator cannot be found:
The service "ugroup_media_personalization.flattening_service"
has a dependency on a non-existent service "validator"
So what am i doing wrong here?
The problem was that the configuration file for the bundle did not specify the
framework:
validation: {enable_annotations: true}
And thus the framework was not loading the validation service. You can simply use:
framework:
validation:
if you want to trigger the loading the validation module, but in my case, i added enable_annotations to make sure the validation using annotations work!
Related
I am beginner to symfony. When I calling get API in symphony,Showing error like below.
RuntimeException
HTTP 500 Internal Server Error
Could not resolve argument $salesteamRepository of "App\Controller\SalesController::index()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?
Its happen because your current class not act as service and everything class that you use in your class not automatically injection by symfony.
If you want automatically dependency injection for your all class that you made you can extend your class to AbstractController, like this
class MyPet extends AbstractController{}
This already mention in official symfony docs.
In Symfony, a controller does not need to be registered as a service.
But if you're using the default services.yaml configuration, and your
controllers extend the AbstractController class, they are
automatically registered as services. This means you can use
dependency injection like any other normal service.
It depends on your version of symfony.
in version 6 (and maybe 5.4 as well) you don't need any extra config other then autowire: true and autoconfigure: true.
in older versions you have to tell the framework to treat your controllers as controllers with method autowiring:
# 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']
https://symfony.com/doc/current/controller/service.html
I have a service defined in services.yaml that has a calls entry since I don't want to specify my dependencies in it's constructor so I use setters.
Now I am trying to point the security.firewalls.main.guard.authenticators.0 configuration entry to it, but if I try to reference it like this: '#App\Service\Authentication\Guard' I get The service "security.authentication.provider.guard.main" has a dependency on a non-existent service "#App\Service\Authentication\Guard".
If I specify just the name of the class as per Symfony's docs: App\Service\Authentication\Guard then there's no exception and the guard works, however the calls section of my definition is completely ignored and thus my setters don't work.
My best guess would be that the security module uses it's own service locator that's somehow disconnected and ignores definitions in services.yaml, which is probably why you won't find examples of the above config entry using simple service names rather than canonical class names.
I'm using the FOSRestBundle and would like to modify the routes it creates.
I intend to add default parameters to each route the Rest Bundle creates. I've looked through the symfony docs on routing and have found nothing covering this use case.
If there is no way I would either have to
modify the FOS Rest Bundle directly
Or copy its route loader code, make my changes, and add it to my own bundle (and not use the Rest Bundle routing at all.)
I don't like either option.
Does Symfony offer a hook that allow for post processing of routes?
FosRestBundle have a custom route loader. If you look in the github repository you will see that the routes are defined in the RestActionReader.php file. So the only solution is to override it and replace the class associate to the service fos_rest.routing.loader.reader.action
We have a project we're migrating to Symfony 3.3 and the new DI configuration. We have a service that used to be passed values from parameters file based on an argument supplied to the command. (bit more complex than this but you get the idea):
$this->service->setOptions($this->getContainer()->getParameter('account_'.$accountId));
With the migration we want to stop making the commands ContainerAwareCommand as we can now use new DI for the services. However, this leaves us stuck working out an alternative method of fetching the parameters when we cannot statically configure them in services.yml
Would it be best for me to just use the Yaml component and read the parameters.yml file in manually for this or is there some other, more appropriate solution?
First of all I'm not a Symfony 2 expert, but I'm trying to understand how the symfony SecurityBundle actually works.
I saw in the docs that a basic implementation to a Session could be registering a EventListener to Kernel events.
Then i started looking at the more complex SecurityBundle to understand it, but I can't find any configuration in security.yml or method inside the Bundle that registers to any event.
I know the Bundle works but I would like to understand how it binds to the Request->Response flow to intercept and filter Requests.
Thank in advance...
The symfony security component has only one kernel listener, of course, on request and as Cerad said in a comment "The various services and registrations are done dynamically since they can be different for different firewalls", basically it's like a custom listener(1) in a kernel listener(2), the definition of kernel listener is in SecurityBundle(3), the magic of creation services from app/config/security.yml is in SecurityExtension(4) of SecurityBundle.
Basically to integrate symfony security in a non symfony project you need to add SecurityBundle in your AppKernel, so the hard part is to have an AppKernel that coexists with other systems.
(1)Symfony\Component\Security\Http\Firewall\ListenerInterface
(2)Symfony\Component\Security\Http\Firewall
(3)/vendor/symfony/symfony/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml
(4)Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension