0

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.

Federkun
  • 36,084
  • 8
  • 78
  • 90
Chetan Nakum
  • 413
  • 6
  • 19

1 Answers1

1

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

        // ...
Federkun
  • 36,084
  • 8
  • 78
  • 90
  • It's fine but I have to call this is in controller so I have write like as : $placeholders = new Placeholders(); So what I have to pass argument in new Placeholders(); method because __construct has one argument. – Chetan Nakum Nov 19 '16 at 13:16
  • 1
    I am not omniscient, you know: `new Placeholders($this->container)` – Federkun Nov 19 '16 at 13:37