How to access base url in twig from symfony 2 console?

I am porting my site, which is located in Cohan on Symfony 2 gradually. Right now I'm writing a backend command in Symfony2, for example. cron to send email notifications.

How can I access the base url in twig. Can I make some configuration so that the access to the URLs in the branch from the console and from the http request is the same?

I already talked about this,

http://symfony.com/doc/current/cookbook/console/sending_emails.html

http://symfony.com/doc/current/cookbook/templating/global_variables.html

the configuration is given here, but it is not mentioned how to access it, I assume that I need to use {{router.request_context.host}}.

But my question is: is there a way to negotiate between the console and HTTP?

eg. {{url}}?

+4
source share
3 answers

It worked for me.

In config.yml:

twig: globals: base_url: "http://www.example.com/" 

In the branch template:

 {{ base_url }} 
+3
source

Add the following parameter to parameters.yml,

 router.request_context.scheme: http router.request_context.host: domain.com base_url: '%router.request_context.scheme%://%router.request_context.host%/' 

and inside the console command controller you can access, for example,

 $host = $this->getContainer()->get('router')->getContext()->getHost(); $scheme = $this->getContainer()->get('router')->getContext()->getScheme(); $baseURL = $this->getContainer()->getParameter('base_url'); 
+4
source

First you must set the URL (you call it base_url) in the route by adding the following lines to /app/config/routing.yml:

 base_url: pattern: / 

After that, you should set the parameters of router.request_context, as indicated in the Symfony cookbook.

Now that everything is set up, you can simply use the same url functions in Twig as on your web pages:

 {{ url('base_url') }} 
+3
source

Source: https://habr.com/ru/post/1493588/


All Articles