简体   繁体   中英

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 ?

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

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

parameters:
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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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