Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 522
Hari Inukollu Avatar asked Mar 11 '16 05:03

Hari Inukollu


1 Answers

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.

like image 120
Oleg Abrazhaev Avatar answered Nov 18 '22 07:11

Oleg Abrazhaev