I have for example CategoriesController.php like this below
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)
);
}
Classes like CategoriesControllel I have a lot, so I think part of it should be in one DefaultController, to not duplicate code. And if i move my code to DefaultController like this:
<?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;
}
}
And in 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)
);
}
After it my application is broken.
( ! ) Catchable fatal error: Argument 1 passed to AppBundle\Controller\DefaultController::__construct() must be an instance of AppBundle\Controller\Translator, instance of...
When i leave empty constructor in CategoriesController, it didn't find any services in the form of private variables. How can I fix it?