In this article, we will give you information about the basic route, named route, and advanced route in laravel 7 and laravel 8.
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. Also, we will see the laravel routing parameter with an example.
Route::get('test', function () {
return 'This is Test Route';
});
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.
use App\Http\Controllers\UserController;
Route::get('/test', [TestController::class, 'index']);
Route::get($url, $callback);
Route::post($url, $callback);
Route::put($url, $callback);
Route::patch($url, $callback);
Route::delete($url, $callback);
Route::options($url, $callback);
Route::match(['get', 'post'], '/', function () {
//
});
Route::any('/', function () {
//
});
Laravel provides default redirection of any URL. If you want to redirect to another URL you may use the 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);
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']);
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', [UserController::class, 'index'])->name('profile');
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...
});
});
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::get('user/{id}', function (App\Models\User $user) {
return $user->email;
});
Example of Rate Limit
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(1500);
});
RateLimiter::for('global', function (Request $request) {
return Limit::perMinute(1000)->response(function () {
return response('Example of Custom response...', 429);
});
});
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:
In this article, we will see how to row group in datatable in jquery. Datatables doesn't have row grouping buil...
Jun-04-2022
In this article, we will see laravel 10 change column names and data types in migration. Here, we will learn about...
Apr-19-2023
In this article, we will see laravel where and orWhere condition example. we will give you a very simple example of...
Jan-11-2021
In this article, we will see localization - laravel localization example. Laravel's localization features provide a...
Nov-06-2020