【发布时间】:2015-08-19 05:21:43
【问题描述】:
我正在尝试使用PhlyRestfully 模块通过 Zend Framework 2 构建 API。问题是尽管我的配置正确,但 ResourceController 无法分派到资源。这是我得到的回应:
The requested controller was unable to dispatch the request.
Controller:
Api\Controller\Locations
No Exception available
我将在下面描述我的配置:
应用程序.php
'modules' => array(
'PhlyRestfully',
'Api',
'Application',
)
Api/config/module.config.php
return array(
'router' => array(
'routes' => array(
'api' => array(
'type' => 'Literal',
'options' => array(
'route' => '/api',
'defaults' => array(
'__NAMESPACE__' => 'Api\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'locations' => array(
'type' => 'Segment',
'options' => array(
'route' => '/locations[/[:id]]',
'controller' => 'Api\Controller\Locations',
'defaults' => array(
'controller' => 'Api\Controller\Locations'
)
),
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy'
),
),
'phlyrestfully' => array(
'resources' => array(
'Api\Controller\Locations' => array(
'listener' => 'Api\Resource\Location',
'route_name' => 'api/locations'
)
)
),
);
Api/Resource/Location.php
namespace Api\Resource;
class Location extends AbstractListenerAggregate
{
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach('fetch', array($this, 'onFetch'));
$this->listeners[] = $events->attach('fetchAll', array($this, 'onFetchAll'));
}
public function onFetchAll(ResourceEvent $e)
{
var_dump($e);
}
public function onFetch(ResourceEvent $e)
{
var_dump($e);
}
}
为了简洁起见,我省略了一些用途和其他行。
网址:/api/locations
路由工作正常,因为 ResourceController 被调用并注入了适当的资源。调用 Listener 上的 attach() 方法,但不是由 Resource 触发事件,而是调用 AbstractRestfulController 的 onDispatch 方法并返回 404,因为我实际上并没有使用 indexAction 定义 LocationsController。
据我了解,PhlyRestfully 通过提供的 ResourceController 模拟这些资源控制器的存在,您不需要实际创建这些控制器类,因为这是模块的全部要点。
有什么想法吗?
【问题讨论】:
标签: api rest zend-framework2