phplaravelpostman

API routes not found


I'm creating a simple CRUD for my internship test as a backend. The test only about API. However, I faced challenges using laravel 11. I can not access the route in api.php in postman, it says 404 not found.

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DivisionController;
use App\Http\Controllers\EmployeeController;

Route::post('/login', [AuthController::class, 'login']);
Route::get('/divisions', [DivisionController::class, 'index']);
Route::get('/employee', [EmployeeController::class, 'index']);
Route::post('/employee', [EmployeeController::class, 'store']);
Route::put('/employee/{id}', [EmployeeController::class,'update']);

I already tried to add config in app.php in bootstrap folder and RouteServiceProvider, but none of it worked.

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

        $middleware->group('api', [
            \Illuminate\Routing\Middleware\ThrottleRequests::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ]);

        $middleware->validateCsrfTokens(except: [
            'api/*', //Menonaktifkan CSRF untuk semua rute API
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();

I'm so clueless about this problem and I already tried bunch of solutions, but nothing worked. Anyone had idea what happened?


Solution

  • I think you may want to try adding the api parameters to the withRouting() call. as explained in the Laravel docs.

    Or possibly try setting up the routing yourself using something like the following snippet from Routing Customization.

    use Illuminate\Support\Facades\Route;
     
    ->withRouting(
        commands: __DIR__.'/../routes/console.php',
        using: function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));
     
            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        },
    )