Laravel Routing

Last Updated: March 2, 2022

Web routes are in routes\web.php and API routes are in routes\api.php

Laravel Route with closure

Route::get('/', function () {
    return "Hello Word";
});

Load a view in a route

    return view("about");
});

Route Verbs

You can match your HTTP request with the route verbs

HTTP GET request

Route::get('/', function () {
    return "Hello Word";
});

HTTP Post request

Route::post('/', function () { 
   // handle post request
});

HTTP Put request

Route::put('/', function () { 
   // handle put request
});

HTTP delete request

Route::delete('/', function () { 
   // handle delete request
});

HTTP any request

Route::any('/', function () { 
   // handle any request
});

How to handle route in Laravel

Two ways to handle routes in Laravel

(1) Using closure

Route::post('/', function () { 
   // handle post request
});

(2) Using controller and method

Route::post('/register', 'UserController@register');

How to name routes in Laravel

When you define a route you can give a name to it

Route::get('/customers/{id}','CustomerController@show')->name('customer.show');

In a view, you can use the route() function to get the URL when you specify the name of the route

<a href="<?php echo route('customer.show',['id'=>8]); ?>"> View </a>

Laravel Route Parameters

When you use route() function you have to define the route parameters

route('customer.show',['id'=>8])
// http://mysite.com/customers/8

route('customer.invoice.show',['customerid'=>8,'invoiceid'=234])
// http://mysite.com/customers/8/invoices/234

route('customer.invoice.show',[8,234])
// http://mysite.com/customers/8/invoices/234

route('customer.invoice.show',['customerid'=>8,'invoiceid'=234,'page'=2])
// http://mysite.com/customers/8/invoices/234?page=2

Laravel Route Group

You can define route groups when they have particular characteristics such as authentication, path prefix, namespace, etc

Route::group( function () {
    Route::get('/products', 'ProductController@index');
    Route::get('/products/12', 'ProductController@show');
});

This group does not do anything instead you can add middleware to the group

Laravel Middleware

Middleware can be used for authenticating user.

Sanctum middleware to authenticate the REST API

Route::group(['middleware' => ['auth:sanctum']], function () {
    Route::get('/products', 'ProductController@index');
    Route::get('/products/12', 'ProductController@show');

});

Rate limiting

Limiting certain number of access to route can be done with the help of throttle middleware

Route::group(['middleware' => ['auth:sanctum','throttle:60,1']], function () {
    Route::get('/products', 'ProductController@index');
    Route::get('/products/12', 'ProductController@show');
});

The first number is the number of times the user tries and send parameter is the number of times to wait before accessing the resource again.

Laravel Route Path Prefix

If the routes have common path segments you can use the path prefix.

Route::prefix('products')->group(['middleware' => ['auth:sanctum','throttle:60,1']], function () {
    Route::get('/', 'ProductController@index');
    Route::get('/12', 'ProductController@show');
});

Laravel Fallback Routes

You can use fallback routes to catch the unmatch routes

Route::fallback(function (){
    
});

Laravel Subdomain Routing

This is the same as Prefix routing.

Route::domain('app.codekayak.net')->group(function () {
    Route::get('/', 'ProductController@index');
    Route::get('/12', 'ProductController@show');
});

Parameterized subdomain routing

Route::domain('{client}.codekayak.net')->group(function ($client) {
    Route::get('/', function ($client) {

    });
    Route::get('/', function ($client) {

    });
});

Laravel Namespace Prefix routing

When you have controllers in the same namespace

Inventory\ProductController@index

Inventory\InvoiceController@index

Following namespace routing will work

Route::namespace('Inventory')->group(function () {
    // App/Http/Controllers/Inventory/ProductController
    Route::get('/products', 'ProductController@index');
    // App/Http/Controllers/Inventory/InvoiceController
    Route::get('/invoices', 'InvoiceController@index');
});
Test