我目前正在完成Zend 3教程。唯一的区别是我使用的是一个Examples表/类而不是Albums。除了每次尝试访问开发服务器时出现以下错误,所有内容似乎都符合规范:
致命错误:未捕获的异常 ' Zend的\的ServiceManager \异常\ ServiceNotFoundException的'与消息 '名称"示例\细分"的插件在插件中找不到 经理Zend \ Router \ RoutePluginManager'在 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的-的ServiceManager \ SRC \ AbstractPluginManager.php:133 堆栈跟踪:#0 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的路由器\ SRC \ SimpleRouteStack.php(280): 的Zend \的ServiceManager \ AbstractPluginManager->获得('实施例\段&#39 ;, 数组)#1 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的路由器\ SRC \ HTTP \ TreeRouteStack.php(201): Zend \ Router \ SimpleRouteStack-> routeFromArray(Array)#2 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的路由器\ SRC \ HTTP \ TreeRouteStack.php(151): Zend \ Router \ Http \ TreeRouteStack-> routeFromArray(Array)#3 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的路由器\ SRC \ SimpleRouteStack.php(142): Zend \ Router \ Http \ TreeRouteStack-> addRoute('示例',数组)#4 C:\网站\ ZEND2 \厂商\ zendframework \ Zend的路由器\ SRC \ SimpleRouteStack.php(86): Zend \ Router \ SimpleRouteStack-> addRoutes(Array)in C:\网站\ ZEND2 \厂商\ zendframework \ Zend的-的ServiceManager \ SRC \ AbstractPluginManager.php 在第133行
ExampleController.php
namespace Example\Controller;
use Example\Model\ExampleTable;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
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 Example\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
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' => Controller\ExampleController::class,
'action' => 'index',
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
'example' => __DIR__ . '/../view',
],
],
];
Module.php
<?php
namespace Example;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
class Module implements ConfigProviderInterface
{
public function getConfig()
{
return include __DIR__ . '\config\module.config.php';
}
// Add this method:
public function getServiceConfig()
{
return [
'factories' => [
Model\ExampleTable::class => function($container) {
$tableGateway = $container->get(Model\ExampleTableGateway::class);
return new Model\ExampleTable($tableGateway);
},
Model\ExampleTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Example());
return new TableGateway('example', $dbAdapter, null, $resultSetPrototype);
},
],
];
}
public function getControllerConfig()
{
return [
'factories' => [
Controller\ExampleController::class => function($container) {
return new Controller\ExampleController(
$container->get(Model\ExampleTable::class)
);
},
],
];
}
}
使用example.php
namespace Example\Model;
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 [
'Zend\Form',
'Zend\Db',
'Zend\Router',
'Zend\Validator',
'Application',
'Example',
];
我已经包含了一切,因为我真的不确定问题出在哪里。任何帮助将不胜感激。
答案 0 :(得分:13)
您的module.config.php
文件需要:
use Zend\Router\Http\Segment;
靠近顶部的声明,用于创建稍后在文件中使用的Segment
别名。目前,因为这不存在,PHP正在您声明的命名空间中寻找该类(所以Example\Segment
),这是不存在的。