微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Laravel API 404错误

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

路线/ 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        |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+

解决方法:

在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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐