2013-05-28 27 views
0

我在運行Phlyrestfully時遇到了問題,基於文檔中的示例。 http://phlyrestfully.readthedocs.org/en/latest/basics/example.html 我已經開始了這個新的zend骨架,並創建了路由和監聽器。 我在任何時候都收到404錯誤。沒有其他錯誤。Phlyrestfully示例不工作 - 404錯誤

return array(
'phlyrestfully' => array(
    'resources' => array(
     'Paste\ApiController' => array(
      'identifier'    => 'Pastes', 
      'listener'    => 'Paste\PasteResourceListener', 
      'resource_identifiers' => array('PasteResource'), 
      'collection_http_options' => array('get', 'post'), 
      'collection_name'   => 'pastes', 
      'page_size'    => 10, 
      'resource_http_options' => array('get'), 
      'route_name'    => 'paste/api', 
      ), 
     ), 
    ), 
'router' => array(
    'routes' => array(
     'paste' => array(
      'type' => 'Literal', 
      'options' => array(
       'route' => '/paste', 
'controller' => 'Paste\PasteController', // for the web UI 
), 
      'may_terminate' => true, 
      'child_routes' => array(
       'api' => array(
        'type' => 'Segment', 
        'options' => array(
         'route'  => '/api/pastes[/:id]', 
         'controller' => 'Paste\ApiController', 
         ), 
        ), 
       ), 
      ), 
     )), 
'view_manager' => array(
    'strategies' => array(
     'ViewJsonStrategy' 
     )) 
); 

監聽文件:

namespace Paste; 

use PhlyRestfully\Exception\CreationException; 
use PhlyRestfully\Exception\DomainException; 
use PhlyRestfully\ResourceEvent; 
use Zend\EventManager\AbstractListenerAggregate; 
use Zend\EventManager\EventManagerInterface; 

class PasteResourceListener extends AbstractListenerAggregate 
{ 
protected $persistence; 

public function __construct(PersistenceInterface $persistence) 
{ 
    $this->persistence = $persistence; 
} 

public function attach(EventManagerInterface $events) 
{ 
    $this->listeners[] = $events->attach('create', array($this, 'onCreate')); 
    $this->listeners[] = $events->attach('fetch', array($this, 'onFetch')); 
    $this->listeners[] = $events->attach('fetchAll', array($this, 'onFetchAll')); 
} 

public function onCreate(ResourceEvent $e) 
{ 
    $data = $e->getParam('data'); 
    $paste = $this->persistence->save($data); 
    if (!$paste) { 
     throw new CreationException(); 
    } 
    return $paste; 
} 

public function onFetch(ResourceEvent $e) 
{ 
    $id = $e->getParam('id'); 
    $paste = $this->persistence->fetch($id); 
    if (!$paste) { 
     throw new DomainException('Paste not found', 404); 
    } 
    return $paste; 
} 

public function onFetchAll(ResourceEvent $e) 
{ 
    return $this->persistence->fetchAll(); 
} 
} 

接口文件:

namespace Paste; 

interface PersistenceInterface 
{ 
public function save(array $data); 
public function fetch($id); 
public function fetchAll(); 
} 

型號文件:

namespace Paste; 

class Module 
{ 
public function getConfig() 
{ 
    return include __DIR__ . '/config/module.config.php'; 
} 

public function getAutoloaderConfig() 
{ 
    return array(
     'Zend\Loader\StandardAutoloader' => array(
      'namespaces' => array(
       __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, 
      ), 
     ), 
    ); 
} 

public function getServiceConfig() 
{ 
    return array('factories' => array(
     'Paste\PasteResourceListener' => function ($services) { 
      $persistence = $services->get('Paste\PersistenceInterface'); 
      return new PasteResourceListener($persistence); 
     }, 
    )); 
} 
} 
+0

如何在Matthews GitRepository上打開一個問題?他可能是唯一能夠幫助的人。 – Sam

回答

0

如文檔中表示,

首先,我將定義一個持久性接口。我在 中這樣做,以便專注於與API相關的部分;你實際上如何堅持你的數據完全取決於你。

所以,你必須自己實現持久層。該示例不適用於複製/粘貼。創建一個實現PersistenceInterface的類並將其注入到偵聽器中。

public function getServiceConfig() 
{ 
    return array('factories' => array(
     'Paste\PasteResourceListener' => function ($services) { 
      $persistence = $services->get('Your\Persistence\Class'); 
      return new PasteResourceListener($persistence); 
     }, 
    )); 
} 
0

路由的控制器配置屬於默認值。該路線是匹配的,但控制器是未找到。原文Pastes來自Matthew的PhlyRestfully文檔的例子缺少這個。

'router' => array('routes' => array(
    'paste' => array(
     'type' => 'Literal', 
     'options' => array(
      'route' => '/paste', 
      'defaults' => array(
       'controller' => 'Paste\PasteController', // for the web UI 
      ) 
     ), 
     'may_terminate' => true, 
     'child_routes' => array(
      'api' => array(
       'type' => 'Segment', 
       'options' => array(
        'route'  => '/api/pastes[/:id]', 
        'defaults' => array(
         'controller' => 'Paste\ApiController', 
        ), 
       ), 
      ), 
     ), 
    ), 
)