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
There are a few solutions:
E_USER_DEPRECATED
reporting. This
just masks the problem. "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. 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:
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:
$db
property to your class.$db = $this->db
(or just use the property!)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With