10

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        |
+--------+-----------+--------------------------------------------+----------------------+-----------------------------------------------------+------------+
13
  • 3
    The api.php routes are already prefixed with /api. You don't need to add this again yourself. With your current set-up, your route is localhost:8888/{projectname}/api/api/v1/meeting Commented Jul 25, 2017 at 5:41
  • I tried also delete api with routes and it shows The same error Commented Jul 25, 2017 at 5:42
  • You don't need /{projectname} in your request url. Commented Jul 25, 2017 at 5:53
  • Tried localhost:8888/api/v1/meeting ? Commented Jul 25, 2017 at 6:04
  • 1
    So, what about localhost:8888/{project-folder}/{project-name}/public/api/v1/meeting ? Commented Jul 25, 2017 at 7:20

6 Answers 6

10

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() {

}
Sign up to request clarification or add additional context in comments.

3 Comments

It's not the problem with this route becouse I change it as you said and still have the same trouble
@KonradUciechowski ok please try it with localhost:8888/{projectname}/index.php/api/v1/meeting
Are you using php artisan serveor apache/nginx/...?
5

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.

3 Comments

You are right I changed it but still got 404 problem. Maybe there is other problem not with routes/api.php file.
ahh can you give me whats your server host config?
Worked for me in Laravel 6.2
2

I would prefer adding Accept application/json header on client side to get JSON response. That’s the most straightforward way, but you would be surprised how many developers forget it, or don’t even know about it.

Comments

0

Maybe you haven't make the api configuration

run

php artisan install:api

for automatically create all config for api

https://laravel.com/docs/11.x/routing#api-routes

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Routes are not listed yet!

[In my case, I fixed it with this update]

  1. Go to I:\php-laravel\laravel-crud\bootstrap\app.php

  2. Your file may look like

    <?php
    
    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Configuration\Exceptions;
    use Illuminate\Foundation\Configuration\Middleware;
    
    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            web: __DIR__ . '/../routes/web.php',
            commands: __DIR__ . '/../routes/console.php',
            health: '/up',
        )
        ->withMiddleware(function (Middleware $middleware): void {
            //
        })
        ->withExceptions(function (Exceptions $exceptions): void {
            //
        })->create();
    
    
  3. If this line is missing, add this line. Your updated file should look

    <?php
    
    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Configuration\Exceptions;
    use Illuminate\Foundation\Configuration\Middleware;
    
    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            web: __DIR__ . '/../routes/web.php',
            commands: __DIR__ . '/../routes/console.php',
            api: __DIR__ . '/../routes/api.php',  // ← ADD THIS LINE
            health: '/up',
        )
        ->withMiddleware(function (Middleware $middleware): void {
            //
        })
        ->withExceptions(function (Exceptions $exceptions): void {
            //
        })->create();
    
    
  4. Now run those commands
    php artisan route:clear

    php artisan cache:clear

    php artisan config:clear

    php artisan optimize:clear

  5. Final command: php artisan route:list

  6. Now you should see your all the available api list

Comments

-1

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.