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?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With