简体   繁体   English

在Symfony 2中,如何在PHP中访问Twig模板化基本URL参数?

[英]In symfony 2 how to access twig templating base url parameter in php?

How do I access base url in service or any other class ? 如何在服务或任何其他类中访问基本URL?

framework:
    templating:
        engines: ['twig']
        packages:
            assets:
                base_urls: ["http://assets.acme.com"] 

Generally: 通常:

$assetUrl = $this->container->get('templating.helper.assets')->getUrl($path, $packageName);

In your case: 在您的情况下:

$baseUrl = $this->container->get('templating.helper.assets')->getUrl('', 'assets');

I suppose your code is in app/config/config/yml 我想你的代码在app/config/config/yml

Look, this code is in my controller : 看,这段代码在我的控制器中:

use Symfony\Component\Yaml\Yaml;
[...]

$configfile = Yaml::parse($this->get('kernel')->getRootDir().'/config/config.yml');
baseurls = $configfile['framework']['templating']['packages']['assets']['base_urls']);

You have to mention base_url into parameter section like 您必须在参数部分提及base_url,例如

parameters: 参数:
base_urls: http://assets.acme.com base_urls: http : //assets.acme.com

and then with the help of container object you can access into any services or class. 然后借助容器对象,您可以访问任何服务或类。

like in Controllar use 像在Controllar中使用

$this->container->getParameter('base_urls'); $ this-> container-> getParameter('base_urls');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM