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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…