'在插件管理器中找不到名为 "ExampleSegment" 的插件 Zend\Router\RoutePluginManager



我目前正在学习Zend3教程。唯一的区别是我使用的是Examples表/类,而不是Albums。除了每次尝试访问开发服务器时都会出现以下错误外,一切似乎都符合规范:

致命错误:未捕获异常带有消息的"Zend\ServiceManager\Exception\ServiceNotFoundException"'在插件中找不到名为"Example\ Segment"的插件中的manager Zend\Router\RoutePluginManager'C: \网站\ ZEND2\vendor\ zendframework\ zend servicemanager\src\AbstractPluginManager.php:133堆栈跟踪:#0C: \Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(280):Zend\ServiceManager\AbstractPluginManager->get("示例\段",阵列)#1C: \Websites\ZEND2\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(201):Zend\Router\SimpleRouteStack->routeFromArray(Array)#2C: \Websites\ZEND2\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(151):Zend\Router\Http\TreeRouteStack->routeFromArray(Array)#3C: \Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(142):Zend\Router\Http\TreeRouteStack->addRoute('example',Array)#4C: \Websites\ZEND2\vendor\zendframework\zend-router\src\SimpleRouteStack.php(86):Zend\Router\SimpleRouteStack->中的addRoutes(Array)C: \网站\ ZEND2\vendor\ zendframework\ zend servicemanager\src\AbstractPluginManager.php在线133

ExampleController.php

namespace ExampleController;
use ExampleModelExampleTable;
use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;
class ExampleController extends AbstractActionController
{
    private $table;
    public function indexAction()
    {
        return new ViewModel([
            'examples' => $this->table->fetchAll(),
        ]);
    }
    public function addAction()
    {
    }
    public function editAction()
    {
    }
    public function deleteAction()
    {
    }
     public function __construct(ExampleTable $table)
    {
        $this->table = $table;
    }
}

ExampleTable.php

<?php 
namespace ExampleModel;
use RuntimeException;
use ZendDbTableGatewayTableGatewayInterface;
class ExampleTable
{
    private $tableGateway;
    public function __construct(TableGatewayInterface $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }
    public function fetchAll()
    {
        return $this->tableGateway->select();
    }
    public function getExample($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(['id' => $id]);
        $row = $rowset->current();
        if (! $row) {
            throw new RuntimeException(sprintf(
                'Could not find row with identifier %d',
                $id
            ));
        }
        return $row;
    }
    public function saveExample(Example $example)
    {
        $data = [
            'name' => $example->name,
            'description'  => $example->description,
            'web_url'  => $example->web_url,
        ];
        $id = (int) $example->id;
        if ($id === 0) {
            $this->tableGateway->insert($data);
            return;
        }
        if (! $this->getExample($id)) {
            throw new RuntimeException(sprintf(
                'Cannot update example with identifier %d; does not exist',
                $id
            ));
        }
        $this->tableGateway->update($data, ['id' => $id]);
    }
    public function deleteExample($id)
    {
        $this->tableGateway->delete(['id' => (int) $id]);
    }
}

module.config.php

<?php
namespace Example;
return [
    // The following section is new and should be added to your file:
    'router' => [
        'routes' => [
            'example' => [
                'type'    => Segment::class,
                'options' => [
                    'route' => '/example[/:action[/:id]]',
                    'constraints' => [
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id'     => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => ControllerExampleController::class,
                        'action'     => 'index',
                    ],
                ],
            ],
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'example' => __DIR__ . '/../view',
        ],
    ],
];

Module.php

<?php
namespace Example;
use ZendDbAdapterAdapter;
use ZendDbAdapterAdapterInterface;
use ZendDbResultSetResultSet;
use ZendDbTableGatewayTableGateway;
use ZendModuleManagerFeatureConfigProviderInterface;
class Module implements ConfigProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . 'configmodule.config.php';
    }
    // Add this method:
    public function getServiceConfig()
    {
        return [
            'factories' => [
                ModelExampleTable::class => function($container) {
                    $tableGateway = $container->get(ModelExampleTableGateway::class);
                    return new ModelExampleTable($tableGateway);
                },
                ModelExampleTableGateway::class => function ($container) {
                    $dbAdapter = $container->get(AdapterInterface::class);
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new ModelExample());
                    return new TableGateway('example', $dbAdapter, null, $resultSetPrototype);
                },
            ],
        ];
    }
     public function getControllerConfig()
    {
        return [
            'factories' => [
                ControllerExampleController::class => function($container) {
                    return new ControllerExampleController(
                        $container->get(ModelExampleTable::class)
                    );
                },
            ],
        ];
    }
}

示例.php

namespace ExampleModel;
class Example
{
    public $id;
    public $name;
    public $description;
    public $web_url
    public function exchangeArray(array $data)
    {
        $this->id     = !empty($data['id']) ? $data['id'] : null;
        $this->name = !empty($data['name']) ? $data['name'] : null;
        $this->description  = !empty($data['description']) ? $data['description'] : null;
        $this->web_url  = !empty($data['web_url']) ? $data['web_url'] : null;
    }
}

modules.config.php

<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
/**
 * List of enabled modules for this application.
 *
 * This should be an array of module namespaces used in the application.
 */
return [
    'ZendForm',
    'ZendDb',
    'ZendRouter',
    'ZendValidator',
    'Application',
    'Example',
];

我已经包括了所有的内容,因为我真的不确定问题出在哪里。任何帮助都将不胜感激。

您的module.config.php文件需要一个:

use ZendRouterHttpSegment;

声明,以创建稍后在文件中使用的Segment别名。目前,因为它不存在,PHP正在您声明的命名空间(所以ExampleSegment)中寻找那个不存在的类。