3

从 Symfony-2.7开始,asset不推荐使用 's 方法的第三个参数(一个指示是否生成绝对 url 的布尔值)。

从来源:

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

所以以兼容 Symfony-3.0 的方式生成它的官方方法变成了:

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

但问题是HttpFoundationExtension提供absolute_url依赖的“请求堆栈”在非http请求的情况下为空。

所以它返回原始路径而不进行任何转换:

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

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

4

1 回答 1

0

尝试将虚拟请求对象推送到 request_stack 服务,并在 parameters.yml 中配置请求上下文,如 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);
于 2015-08-04T21:19:37.483 回答