我有类似下面的CategoriesController.php
class CategoriesController
{
/**
* Translator object.
*
* @var Translator $translator
*/
private $translator;
/**
* Template engine.
*
* @var EngineInterface $templating
*/
private $templating;
/**
* Session object.
*
* @var Session $session
*/
private $session;
/**
* Routing object.
*
* @var RouterInterface $router
*/
private $router;
/**
* Model object.
*
* @var ObjectRepository $model
*/
private $model;
/**
* Form factory.
*
* @var FormFactory $formFactory
*/
private $formFactory;
/**
* CategoriesController constructor.
*
* @param Translator $translator Translator
* @param EngineInterface $templating Templating engine
* @param Session $session Session
* @param RouterInterface $router
* @param ObjectRepository $model Model object
* @param FormFactory $formFactory Form factory
*/
public function __construct(
Translator $translator,
EngineInterface $templating,
Session $session,
RouterInterface $router,
ObjectRepository $model,
FormFactory $formFactory
) {
$this->translator = $translator;
$this->templating = $templating;
$this->session = $session;
$this->router = $router;
$this->model = $model;
$this->formFactory = $formFactory;
}
/**
* Index action.
*
* @return Response A Response instance
*/
public function indexAction()
{
$categories = $this->model->findAll();
return $this->templating->renderResponse(
'AppBundle:Categories:index.html.twig',
array('categories' => $categories)
);
}
像CategoriesControllel这样的类我有很多,所以我认为它应该在一个DefaultController中,而不是重复代码。如果我将我的代码移动到DefaultController,如下所示:
<?php
/**
* Default controller class.
*/
namespace AppBundle\Controller;
/**
* Class DefaultController.
*/
class DefaultController
{
/**
* Translator object.
*
* @var Translator $translator
*/
private $translator;
/**
* Template engine.
*
* @var EngineInterface $templating
*/
private $templating;
/**
* Session object.
*
* @var Session $session
*/
private $session;
/**
* Routing object.
*
* @var RouterInterface $router
*/
private $router;
/**
* Model object.
*
* @var ObjectRepository $model
*/
private $model;
/**
* Form factory.
*
* @var FormFactory $formFactory
*/
private $formFactory;
/**
* CategoriesController constructor.
*
* @param Translator $translator Translator
* @param EngineInterface $templating Templating engine
* @param Session $session Session
* @param RouterInterface $router
* @param ObjectRepository $model Model object
* @param FormFactory $formFactory Form factory
*/
public function __construct(
Translator $translator,
EngineInterface $templating,
Session $session,
RouterInterface $router,
ObjectRepository $model,
FormFactory $formFactory
) {
$this->translator = $translator;
$this->templating = $templating;
$this->session = $session;
$this->router = $router;
$this->model = $model;
$this->formFactory = $formFactory;
}
}
在CategoriesController中:
<?php
/**
* Categories controller class.
*/
namespace AppBundle\Controller;
class CategoriesController extends DefaultController
{
/**
* Index action.
*
* @return Response A Response instance
*/
public function indexAction()
{
$categories = $this->model->findAllWithAds();
if (!$categories) {
throw new NotFoundHttpException(
$this->translator->trans('categories.messages.categories_not_found')
);
}
return $this->templating->renderResponse(
'AppBundle:Categories:index.html.twig',
array('categories' => $categories)
);
}
之后我的申请被打破了。
( ! ) Catchable fatal error: Argument 1 passed to AppBundle\Controller\DefaultController::__construct() must be an instance of AppBundle\Controller\Translator, instance of...
当我在CategoriesController中留下空构造函数时,它没有以私有变量的形式找到任何服务。 我该如何解决?
答案 0 :(得分:1)
我认为问题在于您尚未在services.yml
中将控制器注册为服务。如果您不这样做,Symfony会将其视为常规控制器,并尝试使用默认参数对其进行实例化,从而导致您在错误消息中看到不匹配。
检查the docs,了解如何将控制器定义为服务。