Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel API 404 error

Tags:

php

laravel

api

I am making simple API with Laravel 5.4 and 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/meeting it shows me error 404 page not found. What am I doing wrong?

routes/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:

    +--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+
| 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        |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+
like image 1000
Konrad Uciechowski Avatar asked Jul 25 '17 05:07

Konrad Uciechowski


People also ask

How do I fix REST API 404?

You fix this by opening the listen step in your VSM file, and changing the base path in there, so you don't get a 404 error. You could change that to "/api/" so any api requests are dealt-with, or "/api/retrieveId/" so only retrieveId messages are dealt-with, or "/" so all requests are dealt-with.

What does 404 not found mean in laravel?

If you need to define an additional route to existing resource routes, you must declare it in your web. php BEFORE the resource routes, otherwise it will throw a 404 error for that route.


2 Answers

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

Please change from:

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

}

To

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

}
like image 120
Sagar Arora Avatar answered Oct 29 '22 22:10

Sagar Arora


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

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

like image 33
elegisandi Avatar answered Oct 30 '22 00:10

elegisandi