如何在Helper类中获取根路径 - Symfony2

时间:2016-11-19 11:38:33

标签: php symfony

我的助手类代码如下:

public function getPlaceholders()
{
    try {
        echo $this->getParameter('kernel.root_dir');
    } catch (ParseException $e) {
        printf("Unable to parse the YAML string: %s", $e->getMessage());
    }
    return $this->placeholders;
}

它返回错误如下:

Attempted to call an undefined method named "getParameter" of class "AppBundle\Helper\Placeholders".

请告诉我。

1 个答案:

答案 0 :(得分:1)

注入容器

services:
    app.helper.placeholders:
        class: AppBundle\Helper\Placeholders
        arguments: ['@service_container']

并使用容器的访问器方法参数:

namespace AppBundle\Helper;

use Symfony\Component\DependencyInjection\ContainerInterface;

class Placeholders
{    
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getPlaceholders()
    {
        $root_dir = $this->container->getParameter('kernel.root_dir');

        // ...
相关问题