How to generate url of absolute assets for non-http request

Since Symfony-2.7, the third method argument asset

(a boolean indicating whether to generate an absolute url) has been deprecated.

From sources:

@trigger_error('Generating absolute URLs with the Twig asset() function was deprecated in 2.7 and will be removed in 3.0. Please use absolute_url() instead.', E_USER_DEPRECATED);


So the official way to generate it in a Symfony-3.0 compliant way is:

{{ absolute_url(asset('some/asset.css')) }}


The problem is that HttpFoundationExtension

which provides absolute_url

relies on a "request stack" which is empty in the case of a non-http request.

This way it returns the original path without any conversions:

    if (!$request = $this->requestStack->getMasterRequest()) {
        return $path;

So the question is, how do I create a complete url in a CLI based application?

Important sign : this tip is not relevant if you are using absolute_url



source to share

1 answer

Try pushing the stub object on the request_stack, also set up the request context in the .yml parameters as mentioned in send_emails.html

    $request = new Request();
    // if you use locales
    // to be able to render twig asset absolute paths we have to inject dummy request
    $container->set('request', $request, 'request');




All Articles