2016-06-21 136 views
0

我有例如CategoriesController.php這樣下面擴展__construct不工作

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中留下空的構造函數時,它沒有找到私有變量形式的任何服務。 我該如何解決它?

+1

首先,如果子類需要訪問父類中的屬性(如'$ this-> model'和'$ this-> translator'),則需要將其設置爲'protected'(可訪問父母和孩子)。如果它們被設置爲「private」,則只有父類可以訪問它們。其次,它看起來(通過我可以看到的錯誤消息的一部分),你注入錯誤的參數給構造函數? –

+0

你如何實例化控制器? – topher

+0

......以及@mickadoo在他的回答中說了些什麼。您可能需要在服務容器中註冊所有控制器(DefaultController的子項,如CategoriesController)。不是父母(DefaultController)。 –

回答

1

我認爲問題在於您沒有在您的services.yml中將您的控制器註冊爲服務。如果你不這樣做,Symfony將視爲一個常規控制器,並嘗試使用默認參數實例化它,導致錯誤消息中出現不匹配。

檢查the docs瞭解如何將控制器定義爲服務。

+0

當然我有;) – jhondoe500

+0

然後,我會檢查pooler和magnus-eriksson給出的其他好建議。我也可能建議使用TranslatorInterface類型提示而不是具體的類名。 – mickadoo