简体   繁体   中英

How does Symfony `url()` Twig function determine the Absolute Path when executed in CLI?

Let's pretend that our Twig template contain the following code: {{ absolute_url(path('__Controller__')) }} or {{ url('__Controller__') }} .

When Twig generate the HTML while being executed inside a Symfony (ie: 2.7) command containing the functions above, how does the absolute path is determinated?

It reads it from RequestContext . Which gets automatically populated from request data during HTTP request, but needs to be set explicitly in CLI environment.

You can either configure it manually:

$context = $this->container->get('router')->getContext();
$context->setHost('mydomain.com');
$context->setScheme('https');

Or let Symfony do it automatically for you by setting up the following parameters:

# app/config/parameters.yml
parameters:
    router.request_context.host: mydomain.com
    router.request_context.scheme: https

Symfony docs have an article explaining this: https://symfony.com/doc/2.8/console/request_context.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM