0

根据文档中的示例,我在运行 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);
        },
    ));
}
}
4

2 回答 2

0

路由的控制器配置属于默认值。路由匹配,但控制器未找到。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',
                    ),
                ),
            ),
        ),
    ),
)
于 2013-08-13T14:42:00.200 回答
0

如文档中所述,

首先,我将为持久性定义一个接口。我这样做是为了专注于与 API 相关的部分;您如何实际保存数据完全取决于您。

所以,你必须自己实现持久层。该示例不适用于仅复制/粘贴。创建一个实现的类PersistenceInterface并将其注入您的侦听器。

public function getServiceConfig()
{
    return array('factories' => array(
        'Paste\PasteResourceListener' => function ($services) {
            $persistence = $services->get('Your\Persistence\Class');
            return new PasteResourceListener($persistence);
        },
    ));
}
于 2013-05-30T11:37:46.023 回答