php - php://input 将 url 更改为不包含 index.php 时丢失数据
问题描述
因此,我正在构建一个自定义 API 端点,我需要将数据从 Laravel 发送到该端点。我需要大量的 URL,并希望尽可能干净地做到这一点,同时仍然必须在没有框架的情况下从头开始。
我试图创建以下端点
/api/类别/创建/
现在,我可以使用 Guzzle 发布到 /api/categories/create/index.php 并获得内容file_get_contents('php://input')
就好了。而且我知道当我删除 index.php 时,它仍然会发布到正确的位置,因为我可以将“你好”作为响应发回并且我确实收到了。`
对于为什么file_get_contents('php://input')
可以获取我正在发送的数据,但只有当我在 url 末尾明确指定 index.php 时,我感到有些困惑。
这是我的要求,虽然我不认为错误来自这个......
$client = new Client([
'base_uri' => 'http://example.test/'
]);
$course = [
'title' => $request->title,
'description' => $request->description,
'site' => $request->site
];
$response = $client->post('api/categories/create', [
'json' => ['course' => $course]
]);
有任何想法吗?
解决方案
您需要添加一个.htaccess
文件,该文件定义index.php
为单点访问。
重写模式应该是(类似于)controller/method/id
,例如:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(\d+)$ /index.php?controller=$1&method=$2&id=$3 [L,NC,QSA]
</IfModule>
其次是更多的模式,它们在没有id
和没有的情况下重写method
(默认为索引)。
api/categories/create
需要类似的模式 ~ ^(.*)/(.*)/(.*)$ /index.php?controller=$1&subject=$2&method=$3 [L,NC,QSA]
。也可以用逻辑OR
alike定义可能的值^(.*)/(.*)/(create|read|update|destroy)(/|)$
。
推荐阅读
- php - 有没有一种方法可以使用 php 7.2 版在 codeigniter 3 和 sql server 之间建立连接
- filter - 信号后处理中的非因果滤波
- reactjs - React Firestore 问题:将 url 参数传递给 firestoreconnect
- c++ - c++警告C4114相同类型的限定符使用了不止一次
- javascript - Canvas HTML5:在鼠标移动时显示矩形坐标
- java - groovy-eclipse-compiler 编译但 javac 编译失败
- c# - 创建正确的 JSON 类
- python - 如何找到拟合的多项式特征模型的回归曲线方程
- python - 如何将训练数据拆分成更小的批次以解决内存错误
- java - 在多模块 Spring Boot 应用程序中覆盖 application.yml