Zend Framework 2 - A plugin by the name "getServiceLocator" was not found in the plugin manager

I've been following this tutorial from framework.zend.com

And got this error:

Zend\ServiceManager\Exception\ServiceNotFoundException

A plugin by the name "getServiceLocator" was not found in the plugin manager
Zend\Mvc\Controller\PluginManager


#0 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Controller\PluginManager.php(98): Zend\ServiceManager\AbstractPluginManager->get('getServiceLocat...', NULL)
#1 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Controller\AbstractController.php(258): Zend\Mvc\Controller\PluginManager->get('getServiceLocat...', NULL)
#2 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Controller\AbstractController.php(273): Zend\Mvc\Controller\AbstractController->plugin('getServiceLocat...')
#3 C:\xampp\htdocs\skeleton-application\module\Album\src\Album\Controller\AlbumController.php(33): Zend\Mvc\Controller\AbstractController->__call('getServiceLocat...', Array)
#4 C:\xampp\htdocs\skeleton-application\module\Album\src\Album\Controller\AlbumController.php(33): Album\Controller\AlbumController->getServiceLocator()
#5 C:\xampp\htdocs\skeleton-application\module\Album\src\Album\Controller\AlbumController.php(14): Album\Controller\AlbumController->getAlbumTable()
#6 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Controller\AbstractActionController.php(78): Album\Controller\AlbumController->indexAction()
#7 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(271): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#8 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(151): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Controller\AbstractController.php(105): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#10 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\DispatchListener.php(119): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#11 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(271): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#12 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-eventmanager\src\EventManager.php(151): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 C:\xampp\htdocs\skeleton-application\vendor\zendframework\zend-mvc\src\Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#14 C:\xampp\htdocs\skeleton-application\public\index.php(40): Zend\Mvc\Application->run()
#15 {main}

Here's my AlbumController where the error shows up:

<?php
namespace Album\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class AlbumController extends AbstractActionController
{
    protected $albumTable;

    public function indexAction()
    {
        return new ViewModel(array(
            'albums' => $this->getAlbumTable()->fetchAll(),
        ));
    }

    public function addAction()
    {
    }

    public function editAction()
    {
    }

    public function deleteAction()
    {
    }

    public function getAlbumTable()
    {
        if (!$this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }
}

This is my AlbumTable:

 <?php
 namespace Album\Model;

 use Zend\Db\TableGateway\TableGateway;

 class AlbumTable
 {
     protected $tableGateway;

     public function __construct(TableGateway $tableGateway)
     {
         $this->tableGateway = $tableGateway;
     }

     public function fetchAll()
     {
         $resultSet = $this->tableGateway->select();
         return $resultSet;
     }

     public function getAlbum($id)
     {
         $id  = (int) $id;
         $rowset = $this->tableGateway->select(array('id' => $id));
         $row = $rowset->current();
         if (!$row) {
             throw new \Exception("Could not find row $id");
         }
         return $row;
     }

     public function saveAlbum(Album $album)
     {
         $data = array(
             'artist' => $album->artist,
             'title'  => $album->title,
         );

         $id = (int) $album->id;
         if ($id == 0) {
             $this->tableGateway->insert($data);
         } else {
             if ($this->getAlbum($id)) {
                 $this->tableGateway->update($data, array('id' => $id));
             } else {
                 throw new \Exception('Album id does not exist');
             }
         }
     }

     public function deleteAlbum($id)
     {
         $this->tableGateway->delete(array('id' => (int) $id));
     }
 }

I don't know if there's just a corrupted script file of the plugin or just some version differences because i'm using ver 3.0.2dev i think, and the tutorial is ver 2.4.

Answers


getServiceLocator was deprecated from 2.5 version and has been removed in 3.* so please do not use this in controller.

If you want to inject your dependencies. here it has been explained

Resources that you are using belongs to 2.* version.

Below I have mentioned few resources related to zf3. You can have a look. zf3 official reference and zf3 tutorials

https://github.com/olegkrivtsov/using-zf3-book-samples


Need Your Help

cryptojs: How to generate AES passphrase

encryption passwords aes cryptojs

I want to generate a 256bit password for my AES encryption. When I check the password after the encryption it is different from my initial password. What am I doing wrong? Or is there some security

Multiple Jenkins daemons on different HTTP ports (Mac OS X)

macos continuous-integration jenkins

How to run two daemons of Jenkins, on different HTTP ports (i.e. 7070 and 7071), on Mac OS X system.