Laravel API 404 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45294673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:22:47  来源:igfitidea点击:

Laravel API 404 error

phplaravelapi

提问by Konrad Uciechowski

I am making simple API with Laravel 5.4and I have problem. I created routing and some data for tests but when I testing if routing work properly with Postman by putting localhost:8888/{projectname}/api/v1/meetingit shows me error 404 page not found. What am I doing wrong?

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

routes/api.php

路线/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!";
    }
}

Output of command php artisan route:list:

命令的输出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        |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+

回答by Sagar Arora

as in version 5.4 the api is already added in the end points so no need to add 'api' again in the url.

在 5.4 版中,api 已经添加到端点中,因此无需在 url 中再次添加“api”。

Please change from:

请从:

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

}

To

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

}

回答by elegisandi

In Laravel 5.4, your routes/api.phpshould look like this:

在 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'
    ]);
});

For more info, visit their docs here.

有关更多信息,请在此处访问他们的文档。

回答by Rashi Goyal

Please don't use /api in your routes keep it simple like

请不要在您的路线中使用 /api 保持简单

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

this will list all your routes if there is any error in the route then it will not be listed here.

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

回答by sakrancode

Just try

你试一试

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

don't forget public.

不要忘记public