根据文档中的示例,我在运行 Phlyrestfully 时遇到问题。 http://phlyrestfully.readthedocs.org/en/latest/basics/example.html 我已经将它作为一个新的zend 框架开始,并创建了路由和监听器。我一直收到 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);
},
));
}
}