在树枝中使用服务而不创建树枝延伸

时间:2016-11-11 19:51:05

标签: twig symfony twig-filter

我有一个服务类:

namespace App\DesktopBundle\Lib;

class MediaManager
{
    // media image path
    protected $media_url;

    /**
     * Constructor of class
     * @param $media_url
     */
    public function __construct($media_url) 
    {
        $this->image_url = $media_url;
    }

    public function getImage($media_type, $url){
        return sprintf("%s/%s/%s", $this->image_url, $media_type, $url);
    }
}

/src/App/DesktopBundle/Resources/config/services.yml我有:

services:
    media_manager:
        class: App\DesktopBundle\Lib\MediaManager
        arguments: [%media_url%, "media_type","url"]

/app/config/config.yml我有:

twig:
    globals:
        mediaService: "@media_manager"

在模板中我尝试:

<img src="{{ asset(mediaService.getImage('gifts','150x150')) }}" alt="team member img">

但我有错误:Variable "mediaService" does not exist in。你能帮我吗?提前谢谢。

0 个答案:

没有答案