REST API zend框架2迁移到REST API zend框架3

问题描述 投票:-1回答:1

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

没有ZF3 REST API的文档。

是否可以使用ZF3构建REST API?如果我得到任何一个例子,这将是好的。

php zend-framework3
1个回答
1
投票

检查: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,
                ],
            ],
        ],
    ],
],
© www.soinside.com 2019 - 2024. All rights reserved.