Zend Framework 2 - 在插件管理器中找不到名为“getServiceLocator”的插件

时间:2016-08-31 05:45:40

标签: php zend-framework zend-framework2

我一直在关注framework.zend.com的这个tutorial

得到了这个错误:

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}

这是我的AlbumController,其中显示错误:

<?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;
    }
}

这是我的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));
     }
 }

我不知道插件的脚本文件是否只有损坏,或者只是版本差异,因为我认为我使用的是ver 3.0.2dev,而教程是2.4版本。

1 个答案:

答案 0 :(得分:1)

getServiceLocator已从2.5版本弃用,并已在3. *中删除 所以请不要在控制器中使用它。

如果要注入依赖项。 zf3 official reference

您使用的资源属于2. *版本。

下面我提到了几个与zf3相关的资源。你可以看看。 zf3 tutorialshttps://github.com/olegkrivtsov/using-zf3-book-samples

{{3}}