What is Laravel Routing System ?

The routing system of Laravel is a very fundamental component that defines how your application responds to different HTTP requests. By using it, you can create links between certain URLs to controller actions, closures, or other handlers in the most elegant and intuitive way possible.💥


Laravel Routing


1) Basic Routing

The most basic routing mechanism in Laravel is the capability of defining routes that respond to different HTTP methods.

Route::get('/home', function () { return 'Welcome to Home';});

Route::post('/submit', function () { return 'Form Submitted';});

Route::get() maps HTTP GET requests to a route.

Route::post() maps HTTP POST requests.

Route::put(), Route::patch(), and Route::delete() are available for other HTTP methods.


2) Route Parameters

Laravel allows you to pass parameters to routes, which can be used within your controller or closure.

Route::get('/user/{id}', function ($id) {

    return "User ID: ". $id;

});

Route::get('/user/{name?}', function ($name = null) {

    return $name? "User: $name" : 'No name provided';

});

3) Named Routes

Named routes allow you to use the name of the route instead of the URL path, making the generation of links more dynamic.

Route::get('/user/profile', function () {

     echo "Hello World";

})->name('profile');


4) Route Groups

You can group routes sharing common attributes, such as middleware or namespaces, together using route groups.Route::middleware(['auth'])->group(function () 

    Route::get('/dashboard', function () {

        return view('dashboard');

    });

Route::get('/profile', function () {

        return view('profile');

    });

});

5) Resource Routing

Laravel offers a convenient way of handling CRUD operations using resource controllers.

Route::resource('posts', PostController::class);

GET    /posts           => PostController@index

GET    /posts/create    => PostController@create

POST   /posts           => PostController@store

GET    /posts/{post}    => PostController@show

GET    /posts/{post}/edit => PostController@edit

PUT    /posts/{post}    => PostController@update

DELETE /posts/{post}    => PostController@destroy


6) Controller Routes

You can directly route to controller methods instead of using closures. This is suited for more complex applications where there are several actions.

Route::get('/user/{id}', [UserController::class, 'show']);


7) Route Model Binding

Route model binding is a feature that automatically injects models into your route closures or controllers based on the ID passed in the URL.

Route::get('/user/{user}', function (App\Models\User $user) {

    return $user->name;

});

Laravel will automatically retrieve the User model that corresponds to the {user} parameter.Implicit binding: Laravel automatically resolves the model instance based on the parameter in the route.

8) Route Caching

For production environments, Laravel allows you to cache your routes for improved performance.

Bash

php artisan route:cache

This caches the entire route configuration into a single file, thus speeding up route resolution during requests.


9) Middleware

Middleware lets you filter HTTP requests entering your application. It acts as a layer between the request and the controller.

Route::get('/admin', function () {

    echo "Hello World";

})->middleware('auth');


10) Custom Route Methods

Laravel enables you to specify custom route methods using Route::any(), which will accept any HTTP request method.

Route::any('/submit', function () {

    return 'This can handle any request type';

});

11) Route Fallbacks

You can specify a fallback route that will be executed if no other routes match.

Route::fallback(function () {

    return 'Page not found';

});


Advanced Routing Features

1) Subdomains Routing

You can have routes respond to requests coming on different subdomains.


Route::domain('admin.{account}.example.com')->group(function () {

    Route::get('/dashboard', function ($account) {

        return "Dashboard for account: $account";

    });

});


2) Route Prefixing with Subdomain

Subdomain with prefixing

You may use subdomain and prefixing together for handling the complex URL structures.


Route::domain('admin.{account}.example.com')->prefix('panel')->group(function () {

    Route::get('/dashboard', function ($account) {

return "Admin Panel for $account";

});

});


3) Route Response Caching

Laravel allows caching the routes' responses for performance optimization.


Route::get('/dashboard', function () {

    return Cache::remember('dashboard', 60, function () {

        return view('dashboard');

    });

});

4)  Modifying Route URLs

Sometimes you need to control the URL generation process. You can customize it using Route::url() or route() helpers.


$url = route('profile', ['user' => 1]);


4 Comments

Post a Comment

Previous Post Next Post