doutuzhuohao6449 2016-09-16 01:52
浏览 25

从控制台运行时的Symfony资产URL

I want to add a asset (image) full url (with domain and base) in a twig template when running from a command line (Console Command). It's meant to be sent to email.

The problem is, using absolute_url(asset()) doesn't include the host and base path when running on console.

Also, as specificed in http://symfony.com/doc/current/console/request_context.html, it just works for urls, not for assets.

I've also tried to set the router context when running from console:

$context = $this->getContainer()->get('router')->getContext();
$context->setHost($defaultDomain);
$context->setScheme($scheme);

With no success. Any idea?

  • 写回答

4条回答 默认 最新

  • douxing1353 2016-09-16 07:37
    关注

    In my application, special for this situation with emails, i added global variable into Twig configuration:

    # app/config/config.yml
    twig:
        globals:
            site_addr: "%site_addr%"
    

    And setting this variable in parameters.yml

    # app/config/parameters.yml
    site_addr: https://some.site
    

    And at last, in each email template i used this:

    # AppBundle/Resources/views/Emails/layout.html.twig
    <img src="{{ site_addr ~ asset('SomeAssetName') }}"/>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?