I'm bulding API Laravel 5 application with RESTful controllers.我在控制器中以这种方式定义了destroy
方法:
public function destroy($id)
{
App::abort(404);
}
因为目前我不想处理它。奇怪的是,当我使用这样的代码时,我得到了404
标题,但也从我的 show 方法中获得了输出:
public function show($id)
{
die('show method');
}
因此,当我的资源使用 DELETE 方法时,我得到带有输出show method
的 404 代码。
我 100% 确定我正在启动销毁方法,因为如果我输入我的销毁方法:
public function destroy($id)
{
die('destroy');
}
我将显示带有 200 个状态代码的destroy
我在PhpStorm中对其进行了测试,但也使用此Firefox插件对其进行了测试,并且在两种情况下结果都相同。
问题是 - 这里发生了什么以及如何在没有数据或空数据的情况下只返回 404 代码?
编辑
我已经进一步调查了这个问题以及我的发现。如果我在本地主机上运行我的应用程序:
DELETE http://lara404/test/1
我得到纯粹的 404 错误,因为它应该是。
我复制了完全相同的代码并在 Vagrant 中运行它。我运行网址:
DELETE http://lara404.app/test/1
现在我收到带有abcdef
消息的 404 代码。
我在默认安装中唯一更改的是:
1)在routes.php
开头添加
$router->resource('test','TestController');
2) 将以下代码放入测试控制器:
<?php namespace App'Http'Controllers;
use App'Http'Requests;
use App'Http'Controllers'Controller;
use App;
class TestController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
dd('xxx');
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
return "abcdef";
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
App::abort(404);
}
}
3)在Kernel.php
行中评论:
'Illuminate'Foundation'Http'Middleware'VerifyCsrfToken'
我刚刚也通过全新安装的 Laravel 5(不使用任何特定提交)检查了它,并且发生了完全相同的情况 - 在本地主机上启动的相同代码工作正常,并且在 Vagrant 上运行的相同代码也显示了方法
如果使用 json -
public function destroy($id)
{
return Response::json(null, 404);
}
或者这个如果不使用 json -
public function destroy($id)
{
return Response::make("", 404);
}