7

以下是我的模块配置文件

return array(
'controllers' => array(
    'invokables' => array(
        'RSMobile\Controller\User' => 'RSMobile\Controller\UserController',
    ),
),

// Routes for API calls
'router' => array(
    'routes' => array(

        'rsmobile' => array(
            'type' => 'segment',
            'options' => array(
                'route' => '/rsmobile',
                'defaults' => array(
                    'controller' => 'RSMobile\Controller\User',
                )
            ),

            // Child routes
            'child_routes' => array(
                // Route for "user" API
                'user' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/user[/:id]',
                        'constraints' => array(
                                               'id'     => '[0-9a-zA-Z]+',
                                                              ),
                        'defaults' => array(
                            'controller' => 'RSMobile\Controller\User',
                        )
                    ),
                ),
          )

问题:

我在 UserController 文件中扩展了 AbstractRestfulController 但是,当我使用 www.example.com/rsmobile/user?userid=1 调用它时,它调用get-list而不是get。

路径上的任何灯光都会有所帮助

谢谢

4

2 回答 2

4

我认为您只想使用www.example.com/rsmobile/user?userid=1模式而不是www.example.com/rsmobile/user/1.

在中AbstractRestfulController, 默认情况下$identifierName设置为。id如果它没有id在参数列表中找到,那么它将调用getList()方法。所以你可以做的是在你的控制器(它必须是扩展的AbstractRestfulController)中写下代码:

public function __construct() {
    $this->identifierName = 'userId'; // Override $identifierName value specified in AbstractRestfulController.
}
于 2013-12-31T06:19:18.030 回答
2

我的理解是请求匹配你的/rsmobile/路由而不是/rsmobile/user路由。是吗?

我不知道你如何处理参数userid,但可能你不需要它,而不是www.example.com/rsmobile/user?userid=1你可以调用www.example.com/rsmobile/user/1 它来匹配你的路由,并在你的控制器中给你一个 id 参数。

另外,我认为您缺少'may_terminate' => trueint 子路线。可能你应该是:

        // Child routes
        'child_routes' => array(
            // Route for "user" API
            'user' => array(
                'type' => 'segment',
                 'may_terminate' => true,
                'options' => array(
                    'route' => '/user[/:id]',
                    'constraints' => array(
                                           'id'     => '[0-9a-zA-Z]+',
                                                          ),
                    'defaults' => array(
                        'controller' => 'RSMobile\Controller\User',
                    )
                ),
            ),
于 2013-12-30T20:24:32.437 回答