1
I recently started learning Zend Framework2 from Angularjs through a course from the School of Net. Considering the year of the course, which if I’m not mistaken is 2013, some things have changed in both frameworks. Soon, I encountered a problem using the following code snippet to test the connection to the database and list the records using Doctrine 2:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController {
public function indexAction() {
$em = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$repo = $em->getRepository('Entity\Categoria');
$categorias = $repo->findAll();
return new ViewModel(['categories'=>$categorias]);
}
}
When I run the application, returns the following error:
A plugin by the name "getServiceLocator" was not found in the plugin manager Zend Mvc Controller Pluginmanager
In addition, additional information:
Zend Servicemanager Exception Servicenotfoundexception
And the archive where, apparently, the message comes from:
C: xampp htdocs ZF2 vendor course zendframework zend-servicemanager src Abstractpluginmanager.php:133
As far as I know, the problem stems from the fact that the method getServiceLocator()
was taken from the most recent versions of Zend Framework 2. However, I have no idea how to solve it so that I can continue my tests and pass the course. Someone can give me a light?
Exactly. You can no longer use getServiceLocator, you must create a Factory for your Controller and inject its dependencies into it. In this case the Doctrine Entitymanager.
– Vinícius Fagundes