首页 > 解决方案 > REST API zend 框架 2 迁移到 REST API zend 框架 3

问题描述

目前,我在 Zend Framework 2 中使用 REST API。现在,我想在 ZF3 中迁移 ZF2 REST API。我检查了 ZF3 的 REST API,但没有发现任何东西。

ZF3 REST API 没有文档。

是否可以使用 ZF3 构建 REST API?如果我有任何例子,那就太好了。

标签: phpzend-framework3

解决方案


检查:https ://github.com/multidots/zf3-rest-api或

创建你的休息控制器:

class AlbumRestController extends AbstractRestfulController
    {
        public function get($id)
        {
            return new JsonModel(array("id"));
        }

        public function getList()
        {
            return new JsonModel(
                array(
                    array("id"),
                    array("id"),
                    array("id")
                ));
        }
    }

添加到你的module.config.php:

'controllers' => [
    'factories' => [
        Controller\AlbumRestController::class => InvokableFactory::class,
    ],
],

最后定义你的路线:

'router' => [
    'routes' => [
        'album-rest' => [
            'type' => Segment::class,
            'options' => [
                'route'    => '/album-rest[/:id]',
                'constraints'=> [
                    'id'     => '[0-9]+',
                ],
                'defaults' => [
                    'controller' => Controller\AlbumRestController::class,
                ],
            ],
        ],
    ],
],

推荐阅读