[英]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: 我想将Symfony2 Controller扩展到我正在使用API的项目,但我有一个非对象错误使用getParameter()函数查看我的代码:
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 (..)
FatalErrorException:错误:在(..)中的非对象上调用成员函数getParameter()
I try to use $this->setContainer() but it doesn't work. 我尝试使用$ this-> setContainer()但它不起作用。 Do you have any idea how can I slove this problem?
你知道我怎么能解决这个问题?
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: 您有两种方法可以解决您的情况:
You cant use container in Controller __construct
at reason that when constructor called where is none container set yeat. 您不能在Controller
__construct
中使用容器,因为当构造函数调用where时,无容器集yeat。
You can simply define some simple methods in controller like 你可以简单地在控制器中定义一些简单的方法
class ApiController extends Controller
{
protected function getApiHost()
{
return $this->container->getParameter('api_host');
}
}
I wonder if something crazy like this would work? 我想知道像这样疯狂的东西会起作用吗? Instead of overriding the constructor, override the setContainer method?
而不是覆盖构造函数,重写setContainer方法? 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');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.