基于文档中的示例,我在运行phlyrest时遇到了麻烦。http://phlyrestfully.readthedocs.org/en/latest/basics/example.html我将其作为一个新的zend骨架开始,并创建了路由和侦听器。我总是得到404错误。无其他错误。
return array(
'phlyrestfully' => array(
'resources' => array(
'PasteApiController' => array(
'identifier' => 'Pastes',
'listener' => 'PastePasteResourceListener',
'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' => 'PastePasteController', // for the web UI
),
'may_terminate' => true,
'child_routes' => array(
'api' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/pastes[/:id]',
'controller' => 'PasteApiController',
),
),
),
),
)),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy'
))
);
Listener文件:
namespace Paste;
use PhlyRestfullyExceptionCreationException;
use PhlyRestfullyExceptionDomainException;
use PhlyRestfullyResourceEvent;
use ZendEventManagerAbstractListenerAggregate;
use ZendEventManagerEventManagerInterface;
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(
'ZendLoaderStandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getServiceConfig()
{
return array('factories' => array(
'PastePasteResourceListener' => function ($services) {
$persistence = $services->get('PastePersistenceInterface');
return new PasteResourceListener($persistence);
},
));
}
}
如文档中所述,
首先,我将为持久化定义一个接口。我在做这个以便专注于与API相关的部分;你是如何保存你的数据完全取决于你。
所以,你必须自己实现持久层。这个例子不能只使用复制/粘贴。创建一个实现PersistenceInterface
的类,并将其注入到侦听器中。
public function getServiceConfig()
{
return array('factories' => array(
'PastePasteResourceListener' => function ($services) {
$persistence = $services->get('YourPersistenceClass');
return new PasteResourceListener($persistence);
},
));
}
路由的控制器配置属于默认配置。路由匹配成功,但控制器为notfound。处理步骤马修PhlyRestfully文档中的原始粘贴示例缺少此。
'router' => array('routes' => array(
'paste' => array(
'type' => 'Literal',
'options' => array(
'route' => '/paste',
'defaults' => array(
'controller' => 'PastePasteController', // for the web UI
)
),
'may_terminate' => true,
'child_routes' => array(
'api' => array(
'type' => 'Segment',
'options' => array(
'route' => '/api/pastes[/:id]',
'defaults' => array(
'controller' => 'PasteApiController',
),
),
),
),
),
)