Laravel API 404错误

时间:2017-07-25 05:34:54

标签: php laravel api

我正在使用 Laravel 5.4 制作简单的API,我遇到了问题。我为测试创建了路由和一些数据,但是当我通过localhost:8888/{projectname}/api/v1/meeting测试路由器是否正确使用Postman时,它向我显示error 404 page not found。我做错了什么?

路由/ api.php

<?php    
Route::group(['prefix' => 'v1'], function() {
    Route::resource('meeting', 'MeetingController', [
        'except' => ['edit', 'create']
    ]);

    Route::resource('meeting/registration', 'RegistrationController', [
        'only' => ['store', 'destroy']
    ]);

    Route::post('user', [
        'uses' => 'AuthController@store'
    ]);

    Route::post('user/signin', [
        'uses' => 'AuthController@signin'
    ]);
});

MeetingController

<?php    
namespace App\Http\Controllers;    
use Illuminate\Http\Request;    
use App\Http\Requests;    
class MeetingController extends Controller
{
    public function __construct()
    {
        // $this->middleware('name');
    }

    public function index()
    {
        return "It works!";
    }

    public function store(Request $request)
    {
        return "It works!";
    }

    public function show($id)
    {
        return "It works!";
    }

    public function update(Request $request, $id)
    {
        return "It works!";
    }

    public function destroy($id)
    {
        return "It works!";
    }

}

这个RegistrationController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class RegistrationController extends Controller
{
    public function store(Request $request)
    {
        return "It works!";
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return "It works!";
    }
}

AuthController

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AuthController extends Controller
{
    public function store(Request $request)
    {
        return "It works!";
    }

    public function signin(Request $request)
    {
        return "It works!";
    }
}

输出命令php artisan route:list

    +--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+
| Domain | Method    | URI                                        | Name                 | Action                                              | Middleware |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+
|        | GET|HEAD  | /                                          |                      | Closure                                             | web        |
|        | POST      | api/v1/meeting                             | meeting.store        | App\Http\Controllers\MeetingController@store        | api        |
|        | GET|HEAD  | api/v1/meeting                             | meeting.index        | App\Http\Controllers\MeetingController@index        | api        |
|        | POST      | api/v1/meeting/registration                | registration.store   | App\Http\Controllers\RegistrationController@store   | api        |
|        | DELETE    | api/v1/meeting/registration/{registration} | registration.destroy | App\Http\Controllers\RegistrationController@destroy | api        |
|        | DELETE    | api/v1/meeting/{meeting}                   | meeting.destroy      | App\Http\Controllers\MeetingController@destroy      | api        |
|        | PUT|PATCH | api/v1/meeting/{meeting}                   | meeting.update       | App\Http\Controllers\MeetingController@update       | api        |
|        | GET|HEAD  | api/v1/meeting/{meeting}                   | meeting.show         | App\Http\Controllers\MeetingController@show         | api        |
|        | POST      | api/v1/user                                |                      | App\Http\Controllers\AuthController@store           | api        |
|        | POST      | api/v1/user/signin                         |                      | App\Http\Controllers\AuthController@signin          | api        |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+

5 个答案:

答案 0 :(得分:4)

与版本5.4一样,api已添加到端点中,因此无需在网址中再次添加“api”。

请改变:

Route::group(['prefix' => 'api/v1'], function() {

}

Route::group(['prefix' => 'v1'], function() {

}

答案 1 :(得分:3)

在Laravel 5.4中,您的routes/api.php应如下所示:

<?php

Route::prefix('v1')->group(function () {
    Route::resource('meeting', 'MeetingController', [
        'except' => ['edit', 'create']
    ]);

    Route::resource('meeting/registration', 'RegistrationController', [
        'only' => ['store', 'destroy']
    ]);

    Route::post('user', [
        'uses' => 'AuthController@store'
    ]);

    Route::post('user/signin', [
        'uses' => 'AuthController@signin'
    ]);
});

有关详情,请访问他们的文档here

答案 2 :(得分:0)

我更喜欢在客户端添加 Accept application/json 标头以获得 JSON 响应。这是最直接的方式,但您会惊讶于有多少开发人员忘记了它,甚至不知道它。

答案 3 :(得分:-1)

只需尝试

localhost:8888/{projectname}/public/api/v1/meeting

不要忘记public

答案 4 :(得分:-1)

请不要在路由中使用/ api,使其保持简单

Route::group(['prefix' => 'v1'], function() { //code goes here}
php artisan route:list

这将列出您的所有路线,如果路线中有任何错误,则不会在此处列出。