【问题标题】:ZF2 with PhlyRestfully The requested controller was unable to dispatch the request带有 PhlyRestfully 的 ZF2 请求的控制器无法分派请求
【发布时间】: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


    【解决方案1】:

    删除路由配置中的默认操作。

    【讨论】:

    • 另外,如果您的 REST 路由是另一个需要默认操作的路由的子路由,那么您可以将 REST 默认操作设置为 null 而不是完全删除它
    【解决方案2】:

    当我在控制器中忘记了我的方法的 Action 后缀时,恰好发生了这个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多