如何在 Helper 类中获取根路径 - Symfony2 [英] How to get the root path in Helper Class - Symfony2

查看:27
本文介绍了如何在 Helper 类中获取根路径 - Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的助手类代码如下:

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".

请给我建议.

推荐答案

注入容器

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

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

And use the container's accessor methods for parameters:

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');

        // ...

这篇关于如何在 Helper 类中获取根路径 - Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆