Laravel Routing

Laravel’s routing system (as of version 12) is powerful and flexible, allowing you to map URLs to specific actions in an expressive manner. Here's a complete breakdown:


Basic Routing

web.php (routes\web.php)

Defines how the application responds to HTTP verbs like GET, POST, PUT, DELETE, PATCH, OPTIONS.

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


welcome.blade.php (resources\views\welcome.blade.php)

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Laravel</title>
    </head>
    <body>
    </body>
        <h1>Welcome to Laravel</h1>
        <p>This is a simple welcome page.</p>
    </body>
</html>

Route Files

  • routes/web.php: For web routes with stateful features like sessions and CSRF protections (uses web middleware group).
  • routes/api.php: For stateless API endpoints, prefixed with /api and comes with rate limiting and api middleware.

Redirect & View Routes

Redirect Routes: Quickly redirect one route to another.

Route::redirect('/here', '/there');


View Routes: Serve a Blade view without a controller method.

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

Route Parameters

Capture dynamic values from the URL:

Required Parameter:

Route::get('/users/{id}', function ($id) {
    return "User ID: " . $id;
});


Output:

Optional Parameter:

Route::get('/users/{name?}', function ($name = null) {
    return "User name: " . ($name ?? 'no name');
});
Output with name Output without name

Constraints with Regex:

Route::get('/users/{id}', function ($id) {
    return "User ID: " . $id;
})->where('id', '[0-9]+');
Output with id(0-9) Output with id(a)

Named Routes

Gives routes a name for easier referencing elsewhere:

Route::get('/users/{id}', function ($id) {
    return "User ID: " . $id;
})->name('user.profile');


Can access

<a href="{{ route('user.profile', 5) }}">
  John Deo
</a>

Route Groups

Group your routes for common properties like prefixes:

Route::prefix('admin')->name('admin.')->group(function () {
  Route::get('/dashboard', function(){
    return view('dashboard')
  })->name('dashboard');
});


Can access:

http://my-app.test/admin/dashboard
(Or)
<a href="http://my-app.test/admin/dashboard">
  Dashboard
</a>
(Or)
<a href="{{ route('admin.dashboard') }}">
  Dashboard
</a>

Fallback Routes

Catch-all route for unmatched URIs:

Route::fallback(function () {
  return view('errors.404');
});

Rate Limiting

Define custom rate limiters and attach them to routes:

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('uploads', function ($request) {
  return Limit::perMinute(10);
});

Route::post('/upload', ...)->middleware('throttle:uploads');

List All Routes

To view all registered routes:

php artisan route:list


Add --columns for filtering:

php artisan route:list --method=GET          // Filter by method:
php artisan route:list --name=dashboard      // Filter by route name:
php artisan route:list --path=api/user       //  Filter by URI path:
php artisan route:list --sort=uri            // Sort routes by URI:
php artisan route:list --reverse             // reverse route list

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.