【问题标题】:Zend framework 2 Rest API : Calls getList() instead of get($id) functionZend 框架 2 Rest API:调用 getList() 而不是 get($id) 函数
【发布时间】:2013-12-27 11:59:30
【问题描述】:

以下是我的模块配置文件

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。

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

谢谢

【问题讨论】:

    标签: php rest zend-framework2 zend-rest


    【解决方案1】:

    我认为您只想使用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.
    }
    

    【讨论】:

      【解决方案2】:

      据我了解,该请求与您的 /rsmobile/ 路由匹配,而不是 /rsmobile/user 路由。是吗?

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

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

              // 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',
                          )
                      ),
                  ),
      

      【讨论】:

      • 实际上我想使用 /user?userId=1 而不是 user/1 并且当使用该方法时它调用 getList() 并且如果调用像 /user?userId=1&id=0 那么它调用 get () ,是不是我们必须使用'id'?
      • @akash 的答案就是你所需要的!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      • 2011-04-14
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      相关资源
      最近更新 更多