Cake - 设置路由以支持版本化 API 控制器?
Cake - Setup routing to support versioned API controllers?
我希望能够同时支持来自我的 api 的多个版本化端点,例如:
/api/v1.1/counties/get
/api/v1.2/counties/get
但是在尝试为此实现路由时,我对 Cake 如何想要这个感到有点困惑,因为我不断收到
Error: Controller class Counties could not be found.
尝试 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 的基本控制器,并在需要时使用存根方法扩展它们以覆盖。我的大部分 "fat" 都在模型中。
和src/Controller/Api/V1.1/CountiesController.php有:
namespace App\Controller\Api\V1.1;
class CountiesController extends AppController
{
}
将不胜感激任何见解
您不能在命名空间(文件夹)结构中使用像点这样的字符,因为那是无效的PHP。
您正在寻找的是使用前缀路由和 path
选项,以便您可以连接在名称空间中有效的前缀,并为路线,类似:
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
class 将在
src/Controller/Api/V11/CountiesController.php
命名空间为:
App\Controller\Api\V11
另见
我希望能够同时支持来自我的 api 的多个版本化端点,例如:
/api/v1.1/counties/get
/api/v1.2/counties/get
但是在尝试为此实现路由时,我对 Cake 如何想要这个感到有点困惑,因为我不断收到
Error: Controller class Counties could not be found.
尝试 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 的基本控制器,并在需要时使用存根方法扩展它们以覆盖。我的大部分 "fat" 都在模型中。
和src/Controller/Api/V1.1/CountiesController.php有:
namespace App\Controller\Api\V1.1;
class CountiesController extends AppController
{
}
将不胜感激任何见解
您不能在命名空间(文件夹)结构中使用像点这样的字符,因为那是无效的PHP。
您正在寻找的是使用前缀路由和 path
选项,以便您可以连接在名称空间中有效的前缀,并为路线,类似:
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
class 将在
src/Controller/Api/V11/CountiesController.php
命名空间为:
App\Controller\Api\V11
另见