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()
-方法:
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.
});
要完成'root'-route:$app->get('/',function(){/*...*/});
应该就是这样.
To accomplish a 'root'-route: $app->get('/',function(){/*...*/});
should to exactly this.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tangcbjfe
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20