Cake - 设置路由以支持版本化的API控制器?

时间:2018-01-26 17:48:26

标签: php cakephp url-routing cakephp-3.x api-versioning

我希望能够同时支持来自api的多个版本化端点,例如:

/api/v1.1/counties/get
/api/v1.2/counties/get

但是在尝试为此实现路由时,有点困惑的是Cake如何想要这个,因为我不断获得

  

错误:无法找到控制器类县。

尝试1:

Router::scope('/api', function ($routes) {

    $routes->setExtensions(['json']);
    $routes->fallbacks('DashedRoute');

    $versions = [
        1.1
    ];

    foreach ($versions as $version) {
        $routes->scope('/' . $version, function($routes) {

            $routes->resources('Counties', [
                'controller' => 'Counties',
                'prefix' => 'api',
                'map' => [
                    'get' => [
                        'action' => 'get',
                    ]
                ]
            ]);

        }
    }

});

尝试2:

Router::scope('/api', function($routes) {

    $routes->scope('/v1.1', function($routes) {
        $routes->resources('Counties', [
            'controller' => 'Counties',
            'map' => [
                'get' => [
                    'action' => 'get'
                ]   
            ]   
        ]); 
    }); 

    $routes->connect(
        '/v1.1/counties/get',
        [   
            'controller' => 'Counties',
            'action' => 'get',
        ]   
    );  
});

我目前正在使用的目录结构(仍有待讨论):

src / Controller / Api / V1.1,它将使用来自src / Controller / Api的基本控制器,并使用存根方法扩展它们以在需要时覆盖。我的大部分"脂肪"在模型中。

和src / Controller / Api / V1.1 / CountiesController.php有:

namespace App\Controller\Api\V1.1;

class CountiesController extends AppController
{
}

非常感谢任何见解

1 个答案:

答案 0 :(得分:3)

您不能在命名空间(文件夹)结构中使用像点这样的字符,因为这是无效的PHP。

您正在寻找的是使用前缀路由和path选项,以便您可以连接在命名空间中有效的前缀,并为路由提供自定义路径(URL段),例如:< / p>

Router::prefix('api', function (RouteBuilder $routes) {
    // ...

    $routes->prefix('v11', ['path' => '/v1.1'], function (RouteBuilder $routes) {
        $routes->resources('Counties', [
            'map' => [
                'get' => [
                    'action' => 'get'
                ]  
            ]
        ]);
    });
});

这将连接以下路线(您可以通过bin/cake routes检查已连接的路线in the shell):

+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+
| Route name          | URI template          | Defaults                                                                                         |
+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+
| v11:counties:index  | api/v1.1/counties     | {"controller":"Counties","action":"index","_method":"GET","prefix":"v11","plugin":null}          |
| v11:counties:add    | api/v1.1/counties     | {"controller":"Counties","action":"add","_method":"POST","prefix":"v11","plugin":null}           |
| v11:counties:view   | api/v1.1/counties/:id | {"controller":"Counties","action":"view","_method":"GET","prefix":"v11","plugin":null}           |
| v11:counties:edit   | api/v1.1/counties/:id | {"controller":"Counties","action":"edit","_method":["PUT","PATCH"],"prefix":"v11","plugin":null} |
| v11:counties:delete | api/v1.1/counties/:id | {"controller":"Counties","action":"delete","_method":"DELETE","prefix":"v11","plugin":null}      |
| v11:counties:get    | api/v1.1/counties/get | {"controller":"Counties","action":"get","_method":"GET","prefix":"v11","plugin":null}            |
+---------------------+-----------------------+--------------------------------------------------------------------------------------------------+

中会出现CountiesController
src/Controller/Api/V11/CountiesController.php

命名空间为:

App\Controller\Api\V11

另见