【发布时间】:2019-12-20 03:38:47
【问题描述】:
我正在制作 Laravel 5.8 API (PHP 7.3.6),GET、POST 和 DELETE 函数都正常运行,但我无法让 PUT 正常运行。当我使用 Postman 并向“站点”发出 PUT 请求时,它的行为类似于 GET 请求。 GET、POST 和 DELETE 方法的行为与我预期的一样,即转到正确的路线。
它在 IIS 上的行为是这样的,但现在我将 Docker 与 Apache 和 PHP 7.3.6 一起使用,它具有相同的行为。
感觉一定和 Laravel 有关系,或者我只是用错了 Postman。有什么想法吗?
这是我的 api.php...
Route::get('sites', 'SelectSites@index');
Route::get('sites/{id}', 'SelectSites@show');
Route::put('sites', 'SelectSites@create');
Route::post('sites/{id}', 'SelectSites@update');
Route::delete('sites/{id}', 'SelectSites@delete');
这是创建方法...
public function create(){
$sites = "CREATE";
return $sites;
}
这是 docker-compose.xml...
version: '3'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: laravel-docker
ports:
- 8086:80
这是 VirtualHost 文件...
<VirtualHost *:80>
DocumentRoot /srv/app/public
<Directory "/srv/app/public">
AllowOverride all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Limit GET POST PUT DELETE>
Order allow,deny Allow from all
</Limit>
</VirtualHost>
更新
这在 IIS 中本地运行,我只需要重新启动本地计算机。它也可以在 PHP 网络服务器 php -S 0.0.0.0:8080 中完美运行。我以前从未使用过 PUT,我需要做些什么来配置 Docker 以使用 PUT?
【问题讨论】:
-
尝试在 postman 中为 body 设置 x-www-form-urlencoded。
-
抱歉,我的意思是说我正在使用
x-www-form-urlencoded,我什至在正文中有键/值,以防它需要它们。 -
您是否为 API 路由正确添加了 URL 前缀
/api/sites?你确定你没有两次定义这条路线吗? -
我实际上稍微作弊并从服务中删除了 API 前缀以用于测试目的。 100% 确定它没有被定义两次。
-
您的表单中有常用的form method spoofing 字段吗?尝试添加它,然后执行
POST,就像您真正的 HTML 表单应该做的那样。
标签: laravel apache api put php-7.3