Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

api route not found in laravel [duplicate]

Tags:

php

laravel

I am new to laravel.Firstly Api.php was not initially created in routes.After creating this is my code:

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;


use App\Http\Controllers\Api\StudentController;

Route::middleware('auth:api')->get('/user', function (Request $request) {


    return $request->user();
}

);

Route:: get('student',[StudentController::class,'index']);

Route::fallback(function ()

{
    return response()->view('errors.404', [], 404);


});

But when I am running php artisan route:list student is not there.If I run it in localhost it is showing 404 not found

Can you help me?

like image 826
NN Dipu Avatar asked Aug 31 '25 16:08

NN Dipu


1 Answers

From the Laravel 11 changelog:

The api.php and channels.php route files are no longer present by default, as many applications do not require these files. Instead, they may be created using simple Artisan commands.

Run the artisan command below to set it up.

php artisan install:api

Manually creating the api.php file does not actually register the routes. Running the artisan command adds an entry in your bootstrap/app.php file:

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php', // loads the routes here.
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        //
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();
like image 51
D1__1 Avatar answered Sep 02 '25 06:09

D1__1