Chetan Nakum
Chetan Nakum

Reputation: 433

How to get the root path in Helper Class - Symfony2

My code of Helper Class is as below :

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;
}

It's returning the error as below :

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

Please advice me on it.

Upvotes: 0

Views: 571

Answers (1)

Federkun
Federkun

Reputation: 36924

Inject the container

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

        // ...

Upvotes: 1

Related Questions