Laravel - 404用于摧毁在Vagrant上发射GET

时间:2014-12-30 07:21:46

标签: php rest laravel laravel-5

我正在使用RESTful控制器构建API Laravel 5应用程序。我在控制器中以这种方式定义了方法destroy

public function destroy($id)
{
    App::abort(404);
}

因为目前我不想处理它。奇怪的是当我使用这样的代码时,我得到404标题,但也从我的show方法得到输出:

public function show($id)
{
   die('show method');
}

所以当我对我的资源使用DELETE方法时,我得到了带有输出show method的404代码。

我100%肯定我正在启动destroy方法,因为如果我放入我的destroy方法:

public function destroy($id)
{
    die('destroy');
}

我将显示destroy 200状态代码

我在PhpStorm中进行了测试,但也使用了Firefox addon,在两种情况下结果都相同。

问题是 - 这里发生了什么以及如何在没有数据或空数据的情况下返回404代码?

修改

我已经进一步研究了这个问题以及我发现了什么。如果我在localhost上运行我的应用程序:

DELETE http://lara404/test/1

我得到了纯粹的404错误。

我复制完全相同的代码并在Vagrant中运行它。我运行网址:

DELETE http://lara404.app/test/1

现在我收到带有abcdef消息的404代码。

我在默认安装中更改的唯一内容是:

1)在routes.php

的开头添加
$router->resource('test','TestController');

2)将以下代码放入TestController:

<?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的全新安装(不使用任何特定提交)进行了检查,并且完全相同 - 在localhost上启动的相同代码工作正常,并且在Vagrant上运行的相同代码也显示方法

1 个答案:

答案 0 :(得分:0)

如果使用json -

,则需要这个
public function destroy($id)
{
    return Response::json(null, 404);
}

或者如果不使用json -

public function destroy($id)
{
    return Response::make("", 404);
}