php://input 将 url 更改为不包含索引.php时丢失数据



所以我正在构建一个自定义API端点,我需要从Laravel向其发送数据。我需要大量的URL,并希望尽可能干净地做到这一点,同时仍然必须在没有框架的情况下从头开始。

我正在尝试创建以下端点

/api/categories/create/

现在,我可以使用Guzzle发布到/api/categories/create/index.php并file_get_contents('php://input')很好地获取内容。我知道当我删除索引时.php它仍然发布到正确的位置,因为我可以发回'Hello'作为回复,我确实收到了它。

有点困惑为什么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^(.*)/(.*)/(create|read|update|destroy)(/|)$的可能值。

最新更新