Default GET route with Slim PHP(使用 Slim PHP 的默认 GET 路由)
问题描述
我最近使用 Slim PHP 框架构建了一个小型 API,它运行良好.但是,我想为根/"设置一条 GET 路由,该路由以基本消息进行响应,并让任何其他 GET 请求返回拒绝访问".
I've recently built a small API using the Slim PHP framework and it is working great. I would however like to set a GET route for the root "/" which responds with a basic message and have any other GET requests return an "access denied".
在阅读文档和各种示例后,我一直无法弄清楚如何完成其中任何一项任务.我的项目仅依赖 POST 路由,但能够响应针对根域和任何其他页面的 GET 请求会很棒.
Upon reading both the documentation and various examples, I have not been able to figure out how to accomplish either of these tasks. My project only relies on POST routes but being able to respond to GET requests aimed at both the root domain and any other pages would be fantastic.
代码:
// SLIM INSTANCE
$app = new SlimSlim();
$app->contentType('application/json');
// SLIM ROUTES
$app->group('/core', function() use ($app)
{
$app->post( '/create', 'Create' );
$app->post( '/start', 'Start' );
$app->post( '/stop', 'Stop' );
$app->post( '/delete', 'Delete' );
});
推荐答案
如果你想响应不同的方法,只需使用map()
-Method:
if you want to respond to different methods, just use the map()
-Method:
$app->map('/create', 'Create')->via('GET', 'POST');
要注册一个默认路由",如果没有匹配的路由,它总是回复访问被拒绝",你可以覆盖notFound"-处理程序:
To register a 'default route', which will always reply with 'access denied' if no route matched, you can override the 'notFound'-Handler:
$app->notFound(function () use ($app) {
$app->response->setStatus(403);
//output 'access denied', redirect to login page or whatever you want to do.
});
要完成一个根"路径:$app->get('/',function(){/*...*
本文标题为:使用 Slim PHP 的默认 GET 路由


基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01