【问题标题】:Define API routes in ZF3 having more than one parameters在 ZF3 中定义具有多个参数的 API 路由
【发布时间】:2019-11-22 11:23:03
【问题描述】:

我想在 zend 框架 3 中创建一个有两个参数的 API 路由。我的路线定义如下:

'api' => [
    'child_routes' => [
    /* api/users/<id> */
    'users' => [
        'type'    => Segment::class,
        'options' => [
            'route'       => '/users[/[:id]]',
            'constraints' => [
                'id' => '[0-9]+',
            ],
            'defaults' => [
                'controller' => Api\Controller\UserController::class,
            ],
        ],
        'may_terminate' => true,
        'child_routes' => [
            /* api/users/<uid>/documents/<id> */
            'documents' => [
                'type'    => Segment::class,
                'options' => [
                    'route'       => '/documents[/:id]',
                    'constraints' => [
                        'id'   => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Api\Controller\DocumentController::class,
                    ],
                ],
            ],
        ]
    ]
]

我希望支持的路线是:

api/users
api/users/<user_id>
api/users/<user_id>/documents
api/users/<user_id>/documents/<document_id>

按照上述规则,当我向 api/users//documents 发送 HTTP Get 时,它引用“DocumentsController.php”中的“get”方法,而预期调用“getList”方法。

问题是我应该如何更改我的路由,我的资源定义是否遵循 API 设计原则?

【问题讨论】:

  • 我同意您的一般预期路线设置。我没有在必需参数之前使用可选参数。 api/users//documents 按预期工作,因为用户 ID 参数是可选的。我建议您为单个用户创建一个必需的子路由/:id。给文件的子结构。注意:你总是可以有多个路由到同一个端点,所以你可以这样做:/api/users/id/documents,但也可以:api/documents 并且都使用DocumentController::class-&gt;index 函数(然后处理(不)在函数)

标签: rest api routing zend-framework3


【解决方案1】:

您还应该添加动作名称,您可以将动作名称与控制器一起添加。 因此,控件将转到“api/users/documents”和“index”“api/users”的“getList”

如下更新代码:

'api' => [
    'child_routes' => [
    /* api/users/<id> */
    'users' => [
        'type'    => Segment::class,
        'options' => [
            'route'       => '/users[/:id]',
            'constraints' => [
                'id' => '[0-9]+',
            ],
            'defaults' => [
                'controller' => Api\Controller\UserController::class,
                 'action'    => 'index',
            ],
        ],
        'may_terminate' => true,
        'child_routes' => [
            /* api/users/<uid>/documents/<id> */
            'documents' => [
                'type'    => Segment::class,
                'options' => [
                    'route'       => '/documents[/:id]',
                    'constraints' => [
                        'id'   => '[0-9]+',
                    ],
                    'defaults' => [
                        'controller' => Api\Controller\DocumentController::class,
                        'action'    => 'getList',
                    ],
                ],
            ],
        ]
    ]
]

【讨论】:

    猜你喜欢
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2017-06-27
    • 2016-10-12
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多