Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Zend\Mvc\Controller\AbstractController\getServiceLocator Now Triggers Error #7680

Closed
tomshaw opened this issue Mar 3, 2016 · 3 comments
Closed
Labels

Comments

@tomshaw
Copy link
Contributor

tomshaw commented Mar 3, 2016

I have two questions. First when did getServiceLocator start triggering a dependency injection error. Second where can I find documentation how to correctly inject dependencies into controllers. Normally I register them using Modules.php:

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'Application\Model\User' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new User());
                    return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
                },
                'Application\Model\UserTable' => function ($sm) {
                    $tableGateway = $sm->get('Application\Model\User');
                    $table = new UserTable($tableGateway);
                    return $table;
                }
            )
        );
    }

UserController.php

    protected $userTable;

    public function getUserTable()
    {
        if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('Application\Model\UserTable');
        }
        return $this->userTable;
    }

This is a massive compatibility change and major difference how the original ZF2 documentation explained how thing worked and will have H/Yuge ramifications on production systems for existing clients. I'm currently working on an Apigility application when I noticed the change this morning. I'm afraid to look at our other ZF2 clients production systems and will warn them not to run compser update until a viable solution is tested.

@froschdesign
Copy link
Member

Please read here: zendframework/zend-mvc#89

@tomshaw
Copy link
Contributor Author

tomshaw commented Mar 3, 2016

@froschdesign Thank you for closing the issue immediately/within 10 minutes and failing to answer either question whatsoever.

  • When did getServiceLocator start triggering a dependency injection error.
  • Second where can I find documentation how to correctly inject dependencies into controllers.

Currently as of 03/03/2016 the official Zend documentation explains how to register/inject dependencies the way I've described.

http://framework.zend.com/manual/current/en/user-guide/database-and-models.html

@froschdesign
Copy link
Member

@tomshaw
You can find the explanation behind the link which I have added in my previous comment.

In the "In-depth tutorial for beginners" you can find code examples for this topic.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants