skip to Main Content

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

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;


use AppHttpControllersApiStudentController;

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?

2

Answers


  1. are you using laravel version 11? If yes, please run the php artisan install:api command. Ref: https://laravel.com/docs/11.x/routing#api-routes

    Login or Signup to reply.
  2. 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();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search