Routing - Laravel 7/8 Routing Example

WebSolutionStuff | Nov-01-2020 | Categories : Laravel

In this tutorial i will give you information about basic route, named route and advance route in laravel.

Routing is basic and important components 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. also we will see Laravel Routing Parameter With Example.

 

Basic Routing

 

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 given different files like api.php, web.php etc.

 

 

Example of Laravel 8 Route

 

use App\Http\Controllers\UserController;

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

 

In Laravel Available Basic Route Methods

 

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

 

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

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

 

Laravel Redirect Routes Example

 

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

 

Example of Laravel Route::redirect

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

 Laravel Also provide redirect route with status code but status code is 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

 

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 "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 given url of the route.

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

 

Route Model Binding

 

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

 

Rate Limiting

 

  • Laravel provide 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

 

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

 

Current Route

 

Laravel provide inbuit method for get current information of the route. You can get information about current route using inbuit method of route.

$current_route = Route::current();

$current_route_name = Route::currentRouteName();

$current_route_action = Route::currentRouteAction();

 

Featured Post



Follow us
facebooklogo github instagram twitter