如何在apigility中正确设置子路线?

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

我正在建立我的第一个apigility项目,到目前为止它进展顺利。我遇到了一些路由问题以及如何设置它。

假设我有歌曲,专辑和艺术家(仅针对此问题的虚构示例)。我想做一个像这样的结构:

/artists (GET, POST, PUT, PATCH, DELETE)
/albums (GET, POST, PUT, PATCH, DELETE)
/songs (GET, POST, PUT, PATCH, DELETE)

到目前为止很容易。我对这些终点有过滤,所以我可以说:

/artists/?style=jazz
/albums/?year=2012
etc...

现在,我还想包括这样的子端点:

/artist/12345/albums (GET) - get all albums for artists with id 12345
/artist/12345/songs (GET) - get all songs for artists with id 12345
/albums/98765/songs (GET) - get all songs for album with id 98765

如何以适当的方式在apigility中设置它?

我首先创建了一个单独的服务ArtistAlbum,其中包含route / artists /:artist_id / albums,然后将实体从ArtistAlbumEntity更改为AlbumEntity(基本上就是那个)以及从ArtistAlbumCollection到AlbumCollection的集合。路由标识符名称设置为artist_album_id,但我永远不会使用。你不能删除它。

说实话,这种方式感觉有点黑客。

实际上,上述路线/ artists /:artist_id / albums实际上是/ albums /?artist_id =:artist_id的别名。这是一种过滤器。

如何以正确的方式实现这样的事情?

php rest zend-framework2 apigility
2个回答
0
投票

您可以区分(REST)类/资源之间的不同类型的关系。

  • 协会
  • 聚合
  • 组成

这很好地解释了here on this blogpost但这些关系类型也是nicely defined on Wikipedia

了解这些类型的关系将帮助您了解如何定义模型中的(层次结构),它还可以帮助您组织网址。我不会在这里详细介绍,因为您已经可以在stackoverflow上找到关于关系类型,嵌套资源和休息设计的大量信息。我只会重复一些事情。例如hereherehereherehere但你可以自己找到更多。 只需搜索:“嵌套资源REST设计”

网址设计没有一种普遍接受的解决方案。人们经常对如何处理这个问题有个人偏好。有些人(我不同意)甚至认为适当的休息模型不应该使用嵌套(所以没有分层)的网址,而是使用所有资源的扁平结构。


0
投票

考虑router中的以下Zend 2 module.config.php配置

<?php
[
'router' => [
    'routes' => [
        'api.rest' => [
            'type' => 'Hostname',
            'options' => [
                'route' => 'api.site.dev'
            ],
            'may_terminate' => false,
            'child_routes' => [
                'homeRoute' => [
                    'type' => 'Literal',
                    'options' => [
                        'route'    => '/',
                        'defaults' => [
                            'controller' => 'Api\\V1\\Rest\\Welcome\\Controller',
                        ]
                    ],
                    'may_terminate' => true,
                    'child_routes' => [
                        'appRoute' => [
                            'type'    => 'Literal',
                            'options' => [
                                'route'    => 'app',
                            ],
                            'may_terminate' => false,
                            'child_routes' => [
                                'postsRoute' => [
                                    'type'    => 'Literal',
                                    'options' => [
                                        'route'    => '/posts',
                                        'defaults' => [
                                            'controller' => 'Api\\V1\\Rest\\Post\\Controller',
                                        ],
                                    ],
                                ]
                            ]
                        ]
                    ],
                ]
            ]
        ],
    ]
]
];

然后使用Apigility,如果你想要路由名称运行位于Postapi.site.dev/app/posts资源,只需用api.rest/homeRoute/appRoute/postsRoute调用它

例如:使用像这个$this->url('api.rest/homeRoute/appRoute/postsRoute');这样的URL插件

不知道它是否适用于旧版本,但实际上我使用的是Apigility 1.4.1,它就像一个魅力

© www.soinside.com 2019 - 2024. All rights reserved.