php://在更改url时输入丢失的数据,不包括index.php

问题描述 投票:0回答:1

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

我试图创建以下端点

/ API /类别/创建/

现在,我可以使用Guzzle发布到/api/categories/create/index.php并使用file_get_contents('php://input')获取内容就好了。我知道当我删除index.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]
]);

有任何想法吗?

php laravel api-design
1个回答
1
投票

你需要添加一个.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)(/|)$来定义可能的值。

© www.soinside.com 2019 - 2024. All rights reserved.