Routing - Laravel 7/8 Routing Example

In this tutorial, I will give you information about the basic routes, named routes, and advanced routes in laravel. Routing is a basic and important component of the laravel framework, all laravel routes are determined in the file located as the app/Http/routes.php file. 

Here I will show you routing - laravel 7/8 routing example and how to create routes in laravel 8. Also, we will see laravel routing parameter with example. All Laravel routes are defined in your route files, which are located in the routes directory. These files are automatically loaded by your application's App\Providers\RouteServiceProvider

Basic Routing

Laravel routes accept a URI and a closure, providing a very simple and expressive method of defining routes.

Route::get('test', function () {
	    return 'This is Test Route';
	});

 

 

The Default Route Files

All laravel routes are defined in your route files, which are located in the "routes" folder. And in this route folder laravel gives different files like api.php, web.php, etc.

Example of Laravel 8 Route

 For most applications, you will begin by defining routes in your routes/web.php file. The routes defined in routes/web.php may be accessed by entering the defined route's URL in your browser.

use App\Http\Controllers\UserController;

Route::get('/test', [TestController::class, 'index']);

 

In Laravel Available Basic Route Methods

The router allows you to register routes that respond to any HTTP verb.

Route::get($url, $callback);
Route::post($url, $callback);
Route::put($url, $callback);
Route::patch($url, $callback);
Route::delete($url, $callback);
Route::options($url, $callback);

 

Laravel Route Match and Any Methods

You can match multiple routes using the match route.

Route::match(['get', 'post'], '/', function () {
	    //
	});

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

 

Laravel Redirect Routes Example

Laravel provides default redirection of any URL. If you want to redirect to another URL you may use laravel "Route::redirect" method.

Example of Laravel Route::redirect

Route::redirect('/create', '/index');

 Laravel also provides a redirect route with a status code but the status code is an optional parameter.

Route::redirect('/create', '/index', 301);

 

 

Laravel View Routes Example

If your route only needs to return a view, you may use the "Route::view" method.

Route::view('/index', 'index');

Route::view('/welcome', 'welcome', ['name' => 'name']);

 

Laravel Named Routes Example

Named routes allow the convenient generation of URLs or redirects for specific routes. You can specify a name for a route by using the name method.

Route::get('user/profile', function () {
    	//
	})->name('profile');

Route::get('user/profile', [UserController::class, 'index'])->name('profile');

 

Middleware 

Assign middleware to all routes within a group using the "middleware" method before defining the group.

Route::middleware(['role'])->group(function () {
	    Route::get('user/profile', function () {
		// Uses role middleware...
	    });
	});

 

 

Route Prefixes

The prefixes method is used to prefix the given URL of the route.

Route::prefix('admin')->group(function () {
	    Route::get('users', function () {
		// Matches The "/admin/users" URL
	    });
	});

 

Route Model Binding

When injecting a model ID to a route or controller action, you will often query the database to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes.

Route::get('user/{id}', function (App\Models\User $user) {
    return $user->email;
});

 

Rate Limiting

Laravel provides powerful and customizable rate-limiting services that you may utilize to restrict the amount of traffic for a given route or group of routes.   

Rate limiters are using the "RateLimiter" facade's "for" method.  

Example of Rate Limit

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

	RateLimiter::for('global', function (Request $request) {
	    return Limit::perMinute(1500);
	});

 

 

Rate Limit with Custom Response

If the incoming request exceeds the specified rate limit, a response with a 429 HTTP status code will automatically be returned by Laravel.

RateLimiter::for('global', function (Request $request) {
	    return Limit::perMinute(1000)->response(function () {
		return response('Example of Custom response...', 429);
	    });
	});

 

Current Route

Laravel provides an inbuilt method for getting current information on the route. You can get information about the current route using the inbuilt method of the route.

$current_route = Route::current();

$current_route_name = Route::currentRouteName();

$current_route_action = Route::currentRouteAction();

 


You might also like :

FEATURE POSTS