如何为非http请求生成绝对资产url

时间:2015-07-27 23:33:22

标签: php symfony-2.7 symfony

自Symfony-2.7以来,{@ 1}}方法的第3个参数(表示是否生成绝对URL的布尔值)已被弃用。

来自消息来源:

asset

因此,以Symfony-3.0兼容的方式生成它的官方方式变为:

@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);

但问题是提供{{ absolute_url(asset('some/asset.css')) }} 的{​​{1}}依赖于"请求堆栈"在非http请求的情况下为空。

因此它返回原始路径而没有任何转换:

HttpFoundationExtension

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

所以问题是 - 如何在基于CLI的应用程序中生成完整的URL?

重要注意:http://symfony.com/doc/current/cookbook/console/sending_emails.html当您使用absolute_url帮助时,此建议不实际。

1 个答案:

答案 0 :(得分:0)

尝试将虚拟请求对象推送到request_stack服务,也在parameters.el中配置请求上下文,如sending_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);