How do I configure my services for this?
https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Extension/RoutingExtension.php
I'm guessing it's something like this:
twig.extension.anyName:
class: Bundle\Twig\RoutingExtension
arguments:
- "twig.extension.routing"
tags:
- { name: twig.extension }
...but it errors with:
Catchable Fatal Error: Argument 1 passed to Bundle\Twig\RoutingExtension::__construct() must implement interface Symfony\Component\Routing\Generator\UrlGeneratorInterface, string given...
Valid syntax for referencing other services is # symbol followed by service name. Currently you are passing string "twig.extension.routing" to your service constructor.
If you want pass service instance to your service, it could look something like this
twig.extension.anyName:
class: Bundle\Twig\RoutingExtension
arguments:
- "#twig.extension.routing"
tags:
- { name: twig.extension }
Related
I am using Symfony5 and Twig 1.5 and want to translate the time_diff filter. My service configuration looks like this:
twig.extension:
class: Twig_Extensions_Extension_Date
arguments: ['#translator']
tags:
- { name: twig.extension }
Unfortunately I get the following error message:
Argument 1 passed to Twig_Extensions_Extension_Date::__construct() must be an instance of Symfony\Component\Translation\TranslatorInterface or null, instance of Symfony\Component\Translation\DataCollectorTranslator
The reason for this is that Twig_Extensions_Extension_Date expects Symfony\Component\Translation\TranslatorInterface but DataCollectorTranslator implements Symfony\Contracts\Translation\TranslatorInterface.
How can I pass the correct translator to Twig_Extensions_Extension_Date?
It looks like Twig-extensions was abandoned and you have to write it yourself or use another bundle (https://github.com/twigphp/Twig-extensions/issues/264).
To parse phone number I need to use libphonenumber.phone_number_util in my controller ( Symfony 4) as like as :
$parsed = $this->get('libphonenumber.phone_number_util')->parse($phoneNo);
as we have libphonenumber.phone_number_util in private I wanted to make it public by adding this helper in service as below:
services:
libphonenumber\PhoneNumberUtil:
alias: libphonenumber.phone_number_util
public: true
But this returns Exception and message:
"message": "The \"libphonenumber.phone_number_util\" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.",
"class": "Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException",
If you are using this in a controller method (which I presume you do based on $this->get(...)), you need to
1) Declare your controller as a service and tag it with controller.service_arguments tag
2) Make sure your util service id matches the class name (I suppose it does already). You don't need it to be public - that's and ancient approach
3) Require the util as a parameter to your controller's action method.
E.g.
services:
libphonenumber\PhoneNumberUtil:
alias: libphonenumber.phone_number_util
AppBundle\Controller\MyController:
tags: ['controller.service_arguments']
and
public function validatePhoneAction(Request $request, PhoneNumberUtil $phoneNumberUtil)
{
...
$phoneNumberUtil->parse($request->request->get('phone_number');
...
}
There is a nice Symfony blog post about these changes in dependency management: https://symfony.com/blog/new-in-symfony-3-4-services-are-private-by-default
This gives exception (Tags must be array, check YAML syntax):
services:
application.presentation.twig.extension.nullableDateTime:
class: AppBundle\Application\Presentation\Twig\Extension\NullableDateTime
tags: [twig.extension]
This does not:
services:
application.presentation.twig.extension.nullableDateTime:
class: AppBundle\Application\Presentation\Twig\Extension\NullableDateTime
tags:
- {name: twig.extension}
Why is that? The Symfony docs says to use [] for tags.
I have used the {} syntax somewhere else, maybe only one syntax is accepted?
https://symfony.com/doc/current/service_container/tags.html
I want on my web create cache config with different cache values. I have working example:
// config.yml
parameters:
myValue:
first: 1
second: 2
// services.yml
my_repo:
class: AppBundle\Repository\MyRepository
factory: ["#doctrine.orm.entity_manager", getRepository]
arguments:
- 'AppBundle\Entity\My'
calls:
- [setValue, ["%myValue%"]]
// MyRepository.php
public function setValue($val) {
$this->first = $val['first'];
}
// Inside controller method
$someVariable = $this->get('my_repo')
->someOtherFunction();
But is this way correct? What if another programmer will call repository 'standart' way $em->getRepository('MyRepository')? It will crash on udefined variable... Is there way to do this for example via constructor? Or constructor is bad idea?
I am interested in the yours practice - better solution etc.
Something like
[setValue, ["#=container.hasParameter('myValue') ? parameter('myValue') : array()"]]
Should do the trick. Then just check in your service if the variable injected is empty or not. See the doc for more on the Expression language
I was under the impression I could get the request object like in the code below. Something to do with Dependency Injection.
This below is activated as a service and everything seems to be setup correctly except for the first argument which gives this error:
ErrorException: Catchable Fatal Error: Argument 1 passed to....
namespace Acme\Bundle\BundleName\EventListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class RequestListener
{
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
// etc....
I'm guessing the above is not how you do it?
If you want to declare an event listener on the kernel request, you should declare it that way (notice the tags parameters):
services:
acme.demobundle.listener.request:
class: Acme\Bundle\BundleName\EventListener\RequestListener
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Otherwise, if you want to create just a service, you should declare it that way
services:
acme.demobundle.demo.service:
class: Acme\Bundle\BundleName\Service\DemoService
arguments: [#service_container]
For a service or a listener, I'd advise injecting only the services that are needed.
It's good to know that a service will be initialized on the first call.
A service, a listener and a twig extension can be accessed through the container.
$this->container->get('your.listener.name')
$this->container->get('your.service.name')
$this->container->get('your.extension.name')