3

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?

4
  • 1
    You could define ApiController as a Service. Commented Dec 23, 2013 at 12:29
  • Possible duplicate of stackoverflow.com/questions/9736598/… Commented Dec 23, 2013 at 12:29
  • @Calimero Yes, the theme is similar. But I do not understand the answer. You see what I want to do, whether in symfony is a way to do it? I tried to use setContainer but did not help. Commented Dec 23, 2013 at 12:39
  • answer is quite simple : you cannot use the container in the controller constructor. You have to work with another method to achieve what you want to do. Commented Dec 23, 2013 at 12:45

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Even with injection of the container it still didn't worked worked in the constructor. Currently just calling them in my normal functions, google for quite a time for this specific answer. Is there nowadays a method known how to manage the parameters in a constructor?
0

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

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.