Laravel Routes Explanation

Objectives: Laravel Routes Explanation

Laravel Routes Explanation

Laravel Routes: api.php vs web.php (and Others)

In Laravel, route files define how URLs connect to your code. Laravel separates these routes into different files based on their purpose.

1. Default Route Files in Laravel

  • web.php – For web pages (HTML, sessions, CSRF, views)
  • api.php – For APIs (JSON, stateless, token-based auth)
  • console.php – For Artisan CLI commands
  • channels.php – For real-time broadcasting channels

2. api.php – Full Explanation

Purpose:

  • Defines API endpoints (mostly return JSON)
  • Does not use sessions or CSRF protection
  • Routes are stateless
  • Automatically prefixed with /api

Example:

use Illuminate\Support\Facades\Route;

Route::get('/users', function () {
    return response()->json([
        ['id' => 1, 'name' => 'John Doe'],
        ['id' => 2, 'name' => 'Jane Doe']
    ]);
});

Route::post('/login', [AuthController::class, 'login']);

Request in Postman or browser:

GET http://your-domain.com/api/users

β†’ Returns JSON data.

3. web.php – Full Explanation

Purpose:

  • Routes for HTML pages
  • Uses sessions, cookies, and CSRF protection
  • Best for browser-based apps

Example:

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', [DashboardController::class, 'index'])->middleware('auth');

Request in browser:

GET http://your-domain.com/dashboard

β†’ Loads an HTML page with user session data.

4. Why Not Put API Routes in web.php?

  • CSRF Tokens: API calls may fail without CSRF tokens.
  • Sessions: Extra overhead not needed for APIs.
  • Automatic Prefix: api.php automatically uses /api.
  • Security Model: APIs use token-based auth, not cookies.

5. console.php – Purpose & Example

For defining Artisan commands run in the terminal.

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;

Artisan::command('motivate', function () {
    $this->comment(Inspiring::quote());
});

Run in terminal:

php artisan motivate

6. channels.php – Purpose & Example

For defining real-time broadcasting channels.

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('chat.{roomId}', function ($user, $roomId) {
    return $user->canJoinRoom($roomId);
});

7. Choosing the Right Route File

What You Want Use File Reason
Serve HTML pages & keep user logged in web.php Uses sessions, cookies, CSRF
Serve JSON data to mobile or SPA api.php Stateless, token-based auth
Create terminal commands console.php CLI only
Real-time chat/events channels.php Broadcasting & WebSockets

8. Correct Flow Guide

  1. Browser β†’ HTML β†’ web.php
  2. Browser/Mobile β†’ JSON β†’ api.php
  3. Terminal β†’ Command β†’ console.php
  4. Real-time β†’ Chat/Events β†’ channels.php

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1::

β¬… ➑