Purzynski
Purzynski

Reputation: 1227

Symfony2 Extending Controller getParameter()

I Want to extend Symfony2 Controller to my project that is using API but I am having error of a non object use getParameter() function look at my code:

namespace Moda\CategoryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class ApiController extends Controller
{
    /**
     * @var String 
     */
    protected $_host;

    /**
     * @var String
     */
    protected $_user;

    /**
     * @var String
     */
    protected $_password;

    public function __construct()
    {   
        $this->_host = $this->container->getParameter('api_host');
        $this->_user = $this->container->getParameter('api_user');
        $this->_password = $this->container->getParameter('api_password');

    }
}

And next Controller

namespace Moda\CategoryBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class CategoryController extends ApiController
{
    /**
     * @Route("/category", name="_category")
     * @Template()
     */
    public function indexAction()
    { 
        return array('name' => 'test');
    }

}

And the end, I got this Fatal Error:

FatalErrorException: Error: Call to a member function getParameter() on a non-object in (..)

I try to use $this->setContainer() but it doesn't work. Do you have any idea how can I slove this problem?

Upvotes: 3

Views: 7302

Answers (3)

keyboardSmasher
keyboardSmasher

Reputation: 2811

I wonder if something crazy like this would work? Instead of overriding the constructor, override the setContainer method? I haven't tried it...just thinking out loud.

namespace Moda\CategoryBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;


class ApiController extends Controller
{
    /**
     * @var String
     */
    protected $_host;

    /**
     * @var String
     */
    protected $_user;

    /**
     * @var String
     */
    protected $_password;


    public function setContainer(ContainerInterface $container = null)
    {
        parent::setContainer($container);

        $this->_host = $this->container->getParameter('api_host');
        $this->_user = $this->container->getParameter('api_user');
        $this->_password = $this->container->getParameter('api_password');

    }
}

Upvotes: 0

Alexey B.
Alexey B.

Reputation: 12033

You cant use container in Controller __construct at reason that when constructor called where is none container set yeat.

You can simply define some simple methods in controller like

class ApiController extends Controller
{
    protected function getApiHost()
    {
        return $this->container->getParameter('api_host');
    }
}

Upvotes: 1

shacharsol
shacharsol

Reputation: 2342

If your controller is not defined as service, The constructor execution of the controller is not persisted.

You have two options to solve your situation:

  1. Define the controller as a service and inject the parameters you need using dependency injection.
  2. Add an init method in the controller, or on a parent abstract controller, and call the init method, before the action you need to have these parameters available;

Upvotes: 3

Related Questions