Php 获得;ServiceNotFoundException“;从";“深入”;辅导的
我已经制作了所有Zend Framework 2教程,仅限于“路由”,具体如下: 我的问题是,我已经按照建议在代码中创建了所有内容,但当我在本地Php 获得;ServiceNotFoundException“;从";“深入”;辅导的,php,routing,zend-framework2,zend-servicemanager,Php,Routing,Zend Framework2,Zend Servicemanager,我已经制作了所有Zend Framework 2教程,仅限于“路由”,具体如下: 我的问题是,我已经按照建议在代码中创建了所有内容,但当我在本地http://localhost:8080/blog,运行php-S 0.0.0.0:8080-t public/index.php后可访问 致命错误:未捕获异常“Zend\ServiceManager\exception\ServiceNotFoundException”,消息为“一个名为“Blog\Segment”的插件”在C:_PROJETOS\K
http://localhost:8080/blog
,运行php-S 0.0.0.0:8080-t public/index.php后可访问
致命错误:未捕获异常“Zend\ServiceManager\exception\ServiceNotFoundException”,消息为“一个名为“Blog\Segment”的插件”在C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\Zend servicemanager\src\AbstractPluginManager.php中的插件管理器Zend\Router\RoutePluginManager'中找不到。php:131堆栈跟踪:#0 C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zendframework\Zend Router\src\SimpleRouteStack.php(280):Zend\ServiceManager\AbstractPluginManager->get('Blog\Segment',Array)#1c:_-PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\Zend-router\src\Http\TreeRouteStack.php(201):Zend\router\SimpleRouteStack->routeFromArray(Array)#2 C:#u PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend router\src\Http\TreeRouteStack.php(151):zend\router\Http\TreeRouteStack->routeFromArray(Array)#3 C:#u PROJETOS\Knowledge\zendframework\ZendFramework2\5-InDepth\vendor\zendframework\zendframework\zend\zend->router\src\SimpleRouteStack\SimpleRouteStack.php('deta in C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend servicemanager\src\AbstractPluginManager.php,第131行)
我看不出发生了什么来解决这个问题,很遗憾,我很久没有使用Zend Framework 2编写代码了
我非常感谢在理解和解决它方面的任何帮助
我的模块称为“博客”,其中包括我的文件:
module.config.php
<?php
namespace Blog;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
return [
'service_manager' => [
'aliases' => [
//Model\PostRepositoryInterface::class => Model\PostRepository::class
Model\PostRepositoryInterface::class => Model\ZendDbSqlRepository::class,
],
'factories' => [
Model\PostRepository::class => InvokableFactory::class,
Model\ZendDbSqlRepository::class => Factory\ZendDbSqlRepositoryFactory::class,
],
],
'controllers' => [
'factories' => [
Controller\ListController::class => Factory\ListControllerFactory::class,
],
],
'router' => [
'routes' => [
'blog' => [
'type' => Literal::class,
'options' => [
'route' => '/blog',
'defaults' => [
'controller' => Controller\ListController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'detail' => [
'type' => Segment::class,
'options' => [
'route' => '/:id',
'defaults' => [
'action' => 'detail',
],
'constraints' => [
'id' => '[1-9]\d*',
],
],
],
],
],
],
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
模块.config.php中的-缺少以下命名空间:
use Zend\Router\Http\Segment;
这就是为什么它试图查找Blog\Segment
,因为您忘记了为Segment::class
使用正确的名称空间,从而在当前名称空间中查找该类。在module.config.php
中-缺少以下名称空间:
use Zend\Router\Http\Segment;
这就是为什么它试图查找Blog\Segment
,因为您忘记了为Segment::class
使用正确的名称空间,从而在当前名称空间中查找该类。感谢@Kwido的帮助,它丢失了对Segment的引用!我尝试了,但错误仍然存在。.我更新了代码,并设置了错误通过完整消息。我查看了我的代码,它现在正在运行,感谢解决方案和解释!感谢帮助@Kwido,它缺少对段的引用!我尝试了,但错误仍然存在。我更新了我的代码,并将错误放置在完整消息中。我查看了我的代码,它现在正在运行,感谢解决方案和解释!
<?php
namespace Blog\Factory;
use Interop\Container\ContainerInterface;
use Blog\Model\Post;
use Blog\Model\ZendDbSqlRepository;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Hydrator\Reflection as ReflectionHydrator;
use Zend\ServiceManager\Factory\FactoryInterface;
class ZendDbSqlRepositoryFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new ZendDbSqlRepository(
$container->get(AdapterInterface::class),
new ReflectionHydrator(),
new Post('', '')
);
}
}
<?php
namespace Blog\Model;
interface PostRepositoryInterface
{
/**
* Return a set of all blog posts that we can iterate over.
*
* Each entry should be a Post instance.
*
* @return Post[]
*/
public function findAllPosts();
/**
* Return a single blog post.
*
* @param int $id Identifier of the post to return.
* @return Post
*/
public function findPost($id);
}
<?php
namespace Blog\Model;
class Post
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $text;
/**
* @var string
*/
private $title;
/**
* @param string $title
* @param string $text
* @param int|null $id
*/
public function __construct($title, $text, $id = null)
{
$this->title = $title;
$this->text = $text;
$this->id = $id;
}
/**
* @return int|null
*/
public function getId()
{
return $this->id;
}
/**
* @return string
*/
public function getText()
{
return $this->text;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
}
<h1>Blog</h1>
<?php foreach ($this->posts as $post): ?>
<article>
<h1 id="post<?= $post->getId() ?>"><?= $post->getTitle() ?></h1>
<p><?= $post->getText() ?></p>
</article>
<?php endforeach ?>
use Zend\Router\Http\Segment;