Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

zend framework - PHP Deprecated: You are retrieving the service locator from within the class ZFToolControllerModuleController

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 ZFToolControllerModuleController. Please be aware that ServiceLocatorAwareInterface is deprecated and will be removed in version 3.0, along with the ServiceLocatorAwareInitializer. ...

I am using Ubuntu

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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('DbApplicationAdapter');

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 ZendDbAdapterAdapterInterface;
use ZendMvcControllerAbstractActionController;

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('DbApplicationAdapter'));
    }
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...