PHP Deprecated: You are retrieving the service locator from within the class ZFTool\Controller\ModuleController

笑着哭i 提交于 2019-11-27 16:38:12

问题


I have installed zend tools using composer

$ composer require zendframework/zftool:dev-master 

zftool has been installed and when I run php /vender/bin/zf.php modules list it's throwing warning

PHP Deprecated: You are retrieving the service locator from within the class ZFTool\Controller\ModuleController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. ...

I am using Ubuntu


回答1:


There are a few solutions:

  • In your error_reporting, disable E_USER_DEPRECATED reporting. This just masks the problem.
  • Pin to an earlier version of zend-mvc (e.g., composer require "zendframework/zend-mvc:~2.6.0" will pin specifically to the 2.6 series, and will not install the 2.7 series). This, again, just masks the problem, and will potentially leave your application insecure if security patches are applied to a later minor release of zend-mvc.
  • Fix your code to no longer use getServiceLocator(). This is the recommended path. The way to accomplish this latter point is to ensure that all dependencies for your controller are injected during instantiation.

This will mean:

  • You need to create factories for your controllers.
  • You will need to update your controllers to accept dependencies in their constructors that were previously pulled from getServiceLocator(). As an example, let's say you had something like this in your controller:

$db = $this->getServiceLocator()->get('Db\ApplicationAdapter');

You would change your code as follows:

  • Add a $db property to your class.
  • Update your constructor to accept the database adapter, and assign it to the property.
  • Change the above line to simply $db = $this->db (or just use the property!)
  • Add a factory for your controller, if one does not currently exist.

So:

use Zend\Db\Adapter\AdapterInterface;
use Zend\Mvc\Controller\AbstractActionController;

class YourController extends AbstractActionController
{
    private $db;

    public function __construct(AdapterInterface $db)
    {
        $this->db = $db;
    }

    public function someAction()
    {
        $results = $this->db->query(/* ... */);
        /* ... */
    }
}

Your factory would look something like this:

class YourControllerFactory
{
    public function __invoke($container)
    {
        return new YourController($this->get('Db\ApplicationAdapter'));
    }
}

In your application or module configuration, you would map this factory to your controller:

return [
    'controllers' => [
        'factories' => [
            YourController::class => YourControllerFactory::class,
        /* ... */
        ],
        /* ... */
    ],
    /* ... */
];
];

This may seem like a lot of steps. However, it ensures your code has no hidden dependencies, improves the testability of your code, and allows you to do cool things like substitute alternatives via configuration.



来源:https://stackoverflow.com/questions/35933113/php-deprecated-you-are-retrieving-the-service-locator-from-within-the-class-zft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!