使用旧 api 版本时 Slim Framework 显示信息
Slim Framework show info when using old api version
我正在关注 this guide 使用 Slim Groups 来控制我的 API。我只是想知道是否有可能捕获对旧版本的所有调用,而无需为每个函数都执行此操作。就像一个通配符,你知道吗?
例如我从 v1 更新到 v2,所以调用:myapi.com/v1/user/1
应该 return:您使用的是旧的 API 版本。
但我不想这样做(对于每个函数):
$app->group('/v1', function () use ($app) {
$app->group('/user', function () use ($app) {
$app->get('/:id', function ($id) {
echo "You are using an old version.";
});
});
});
但更像这样(他忽略了所有子部分和参数):
$app->get('/v1/*', function ($id) {
echo "You are using an old version.";
});
这只是一个私有的 API,只有知道如何处理它的我的应用程序才会使用,所以请不要关心向后兼容性 ^^
如果您只想 return 任何 /v1/anything/can/be/here
路由的消息,那么您可以使用通配符路由 (docs)。
$app->get('/v1/:anything+', function () use ($app) {
$app->halt(400, 'You are using old API');
});
如果你想保持旧的 API 仍然有效,但想修改响应以包含消息,请使用组中间件。
<?php
$app->group('/v1', function () use $(app) {
// this is the middleware
echo 'You are using old API';
// $app->stop(); // uncomment to stop HERE
}, function () use ($app) {
$app->get('/:id', function ($id) {
// logic for the route
});
});
我用过:
$app->group('/v1', function() {
$this->any('', function ($request, $response, $args) {
return $response->withJson(
array("error" => TRUE, "msg" => "You are using old API"),
400 // status code
);
});
});
我正在关注 this guide 使用 Slim Groups 来控制我的 API。我只是想知道是否有可能捕获对旧版本的所有调用,而无需为每个函数都执行此操作。就像一个通配符,你知道吗?
例如我从 v1 更新到 v2,所以调用:myapi.com/v1/user/1
应该 return:您使用的是旧的 API 版本。
但我不想这样做(对于每个函数):
$app->group('/v1', function () use ($app) {
$app->group('/user', function () use ($app) {
$app->get('/:id', function ($id) {
echo "You are using an old version.";
});
});
});
但更像这样(他忽略了所有子部分和参数):
$app->get('/v1/*', function ($id) {
echo "You are using an old version.";
});
这只是一个私有的 API,只有知道如何处理它的我的应用程序才会使用,所以请不要关心向后兼容性 ^^
如果您只想 return 任何 /v1/anything/can/be/here
路由的消息,那么您可以使用通配符路由 (docs)。
$app->get('/v1/:anything+', function () use ($app) {
$app->halt(400, 'You are using old API');
});
如果你想保持旧的 API 仍然有效,但想修改响应以包含消息,请使用组中间件。
<?php
$app->group('/v1', function () use $(app) {
// this is the middleware
echo 'You are using old API';
// $app->stop(); // uncomment to stop HERE
}, function () use ($app) {
$app->get('/:id', function ($id) {
// logic for the route
});
});
我用过:
$app->group('/v1', function() {
$this->any('', function ($request, $response, $args) {
return $response->withJson(
array("error" => TRUE, "msg" => "You are using old API"),
400 // status code
);
});
});