0

How I can call method getServiceManager in constructor of controller?

If I in action method it's ok

  public function __construct()
    {

      $this->logger =  $this->getEvent()->getApplication()->getServiceManager()->get("Zend\Log\Logger\Debug");

    }

, but in constructor getServiceManager give error:

Call to a member function getServiceManager() on null in C:\htdocs\test-zend2\module\Application\src\Controller\IndexController.php on line 32.
yavafree
  • 27
  • 6
  • You should inject dependencies in the constructor, so if you need whole ServiceManager you should inject it (that is not recommended)... – tasmaniski Apr 09 '17 at 17:42
  • I see this in article, but after zend1 i don't understand why it's so hard? Thanks – yavafree Apr 09 '17 at 19:22
  • Yap that's true :/ Basically this is the answer and mini tutorial how to do it http://stackoverflow.com/questions/42168619/servicemanager-in-zf3/42176170#42176170 – tasmaniski Apr 09 '17 at 20:06
  • Thanks tasmaniski)), work, but why )) thanks! – yavafree Apr 09 '17 at 22:41
  • The serviceManager use to be accessible from the controller in ZF2 but it has been deprecated and remove a while ago. This is anti-pattern right now, and you should seek for alternatives instead. – Unex Apr 10 '17 at 15:13
  • Possible duplicate of [ServiceManager in ZF3](https://stackoverflow.com/questions/42168619/servicemanager-in-zf3) – halfpastfour.am May 22 '17 at 19:15
  • If your problem has been solved, please consider posting an answer to your questions and accepting it yourself or deleting the question. – halfpastfour.am May 22 '17 at 19:16

0 Answers0