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;
    }

      

https://github.com/symfony/symfony/blob/2.7/src/Symfony/Bridge/Twig/Extension/HttpFoundationExtension.php#L59

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

Important sign : http://symfony.com/doc/current/cookbook/console/sending_emails.html this tip is not relevant if you are using absolute_url

helper.

+3


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
    $request->setLocale("en");
    // to be able to render twig asset absolute paths we have to inject dummy request
    $container->enterScope('request');
    $container->set('request', $request, 'request');
    $container->get('request_stack')->push($request);

      

0


source







All Articles