2016-12-30 3 views
0

"심층"튜토리얼에서 라우팅 "ServiceNotFoundException"정확히 내 코드에서 https://docs.zendframework.com/tutorials/in-depth-guide/understanding-routing/얻기 나는 모든 젠드 프레임 워크 2 자습서 "라우팅"에서 중지했습니다

내 문제는 내가이 만든 모든 것을 제안하지만이 오류에 직면하고있어 나는이 php -S 0.0.0.0:8080 -t public public/index.php를 실행 한 후 접근, 내 로컬 http://localhost:8080/blog의 경로를하려고하면

Fatal error: Uncaught exception 'Zend\ServiceManager\Exception\ServiceNotFoundException' with message 'A plugin by the name "Blog\Segment" was not found in the plugin manager Zend\Router\RoutePluginManager' in C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php:131 Stack trace: #0 C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-router\src\SimpleRouteStack.php(280): Zend\ServiceManager\AbstractPluginManager->get('Blog\Segment', Array) #1 C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(201): Zend\Router\SimpleRouteStack->routeFromArray(Array) #2 C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-router\src\Http\TreeRouteStack.php(151): Zend\Router\Http\TreeRouteStack->routeFromArray(Array) #3 C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-router\src\SimpleRouteStack.php(142): Zend\Router\Http\TreeRouteStack->addRoute('deta in C:_PROJETOS\Knowledge\ZendFramework2\5-InDepth\vendor\zendframework\zend-servicemanager\src\AbstractPluginManager.php on line 131

내가 문제를 해결하는 일이 뭐죠 볼 수없는, 오랫동안 내가 돈이 있었다 불행히도 Zend Framework 2로 코드를 작성하지 마십시오.

이해하고 해결하는 데 도움이 되었기 때문에 매우 감사드립니다. ListController.php

<?php 
namespace Blog\Controller; 

use Blog\Model\PostRepositoryInterface; 
use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use InvalidArgumentException; 

class ListController extends AbstractActionController 
{ 
    /** 
    * @var PostRepositoryInterface 
    */ 
    private $postRepository; 

    public function __construct(PostRepositoryInterface $postRepository) 
    { 
     $this->postRepository = $postRepository; 
    } 

    public function indexAction() 
    { 
     return new ViewModel([ 
      'posts' => $this->postRepository->findAllPosts(), 
     ]); 
    } 

    public function detailAction() 
    { 
     $id = $this->params()->fromRoute('id'); 

     try { 
      $post = $this->postRepository->findPost($id); 
     } catch (\InvalidArgumentException $ex) { 
      return $this->redirect()->toRoute('blog'); 
     } 

     return new ViewModel([ 
      'post' => $post, 
     ]); 
    } 
} 

ZendDbSqlRepository.php

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', 
     ], 
    ], 
]; 

:

내 모듈은 그 안에이 내 파일입니다, "블로그"라고하고

<?php 
namespace Blog\Model; 

use InvalidArgumentException; 
use RuntimeException; 
use Zend\Hydrator\HydratorInterface; 
use Zend\Db\Adapter\AdapterInterface; 
use Zend\Db\Adapter\Driver\ResultInterface; 
use Zend\Db\ResultSet\HydratingResultSet; 
use Zend\Db\Sql\Sql; 

class ZendDbSqlRepository implements PostRepositoryInterface 
{ 
    /** 
    * @var AdapterInterface 
    */ 
    private $db; 

    /** 
    * @var HydratorInterface 
    */ 
    private $hydrator; 

    /** 
    * @var Post 
    */ 
    private $postPrototype; 

    public function __construct(
     AdapterInterface $db, 
     HydratorInterface $hydrator, 
     Post $postPrototype 
    ) { 
     $this->db   = $db; 
     $this->hydrator  = $hydrator; 
     $this->postPrototype = $postPrototype; 
    } 

    /** 
    * Return a set of all blog posts that we can iterate over. 
    * 
    * Each entry should be a Post instance. 
    * 
    * @return Post[] 
    */ 
    public function findAllPosts() 
    { 
     $sql  = new Sql($this->db); 
     $select = $sql->select('posts'); 
     $statement = $sql->prepareStatementForSqlObject($select); 
     $result = $statement->execute(); 

     if (! $result instanceof ResultInterface || ! $result->isQueryResult()) { 
      return []; 
     } 

     $resultSet = new HydratingResultSet($this->hydrator, $this->postPrototype); 
     $resultSet->initialize($result); 
     return $resultSet; 
    } 

    /** 
    * {@inheritDoc} 
    * @throws InvalidArgumentException 
    * @throws RuntimeException 
    */ 
    public function findPost($id) 
    { 
     $sql  = new Sql($this->db); 
     $select = $sql->select('posts'); 
     $select->where(['id = ?' => $id]); 

     $statement = $sql->prepareStatementForSqlObject($select); 
     $result = $statement->execute(); 

     if (! $result instanceof ResultInterface || ! $result->isQueryResult()) { 
      throw new RuntimeException(sprintf(
       'Failed retrieving blog post with identifier "%s"; unknown database error.', 
       $id 
      )); 
     } 

     $resultSet = new HydratingResultSet($this->hydrator, $this->postPrototype); 
     $resultSet->initialize($result); 
     $post = $resultSet->current(); 

     if (! $post) { 
      throw new InvalidArgumentException(sprintf(
       'Blog post with identifier "%s" not found.', 
       $id 
      )); 
     } 

     return $post; 
    } 
} 

ZendDbSqlRepositoryFactory.php

<?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('', '') 
     ); 
    } 
} 

PostRepositoryInterface.php

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

Post.php

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

index.phtml

당신의 module.config.php
<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 ?> 

답변

3

- 누락 다음 네임 스페이스 : 왜 당신이 Segment::class에 적합한 네임 스페이스를 사용하는 것을 잊었다 따라서 현재 네임 스페이스 내에서 클래스를 찾고으로 Blog\Segment을 찾기 위해 시도하고있다

use Zend\Router\Http\Segment; 

.

+0

도움 주셔서 감사합니다. @Kwido, 세그먼트에 대한 참조가 누락되었습니다. 시도했지만 오류가 지속됩니다. 내 코드를 업데이트하고 전체 메시지로 오류를 배치했습니다. – RPichioli

+0

코드를 검토 한 결과 해결책과 설명 덕분에 지금 실행 중입니다! – RPichioli