Your cart is currently empty!
How does Laravel Routing work and Insight of Routing?
Hi guys,
Today we are studying Laravel Routing. Routing is the process of directing incoming web requests to the appropriate controller methods.
Summary of Laravel Routing
Basic Routing:
The basic routing structure is a URL and a Closure. You can find all routes on the routes directory, and these are automatically loaded. The web.php file contains Routes that are for the web interface. The web middleware groups hold multiple routes. The api.php file contains stateless routes and is assigned by API middleware.
Some of the HTTP verbs which given below:
Route::get()
Route::post()
Route::put()
Route::patch()
Route::delete()
Route::options()
Sometimes, we are dealing with multiple HTTP verbs. So in that case, a match and any method are taking place. For example,
Route::match(['get','post'], '/', function(){
echo 'match method';
});
Route::any('/', function(){
echo 'any method';
});
Redirect Routes:
If you are defining a route that redirects to another URI, the redirect method takes place.
Route::redirect('/user','/there');
View Routes:
The view method accepts a URI as the first parameter, the second parameter as the view name, and the third parameter as an array of data, and it is optional.
Route::view('/welcome','welcome');
Route::view('/welcome','welcome', ['name'=>'john']);
Routing Parameter:
Required Parameter:
Sometimes, parameters will pass through the URL for convenience. Route parameters are always enclosed with {} curly braces.
Route::get('user/{id}',function($id){
return 'User '.$id;
});
Optional Parameter:
The optional parameter is a parameter that does not stop the execution. It acts as an optional parameter. The ‘?’ operator is used for the Optional parameter.
Route::get('user/{name?}', function($name = 'john'){
return $name;
});
Regular Expression constraints:
Regular Expression is used to format parameters. The where method accepts the name of the parameter and a regular expression defining what the parameter should be.
Route::get('user/{name}',function($name){
})->where('name','[A-Za-z]+');
Named Route:
The name assigns to a specific route that it will easily remember.
Route::get('user/profile', 'UserController@show')->name('profile');
Using the named route feature, we can access the URL from the name of the route using the global route function. If the URL contains parameters, you should pass an associative array to the Global route function. For example,
Route::get('user/{id}/info',function($id){
echo $id;})->name('profile');
route('profile',['id'=>1]);
Route groups:
Route groups allow you to share route attributes, such as middleware or namespaces, across a large number of routes without needing to define those attributes on each URL.
Middleware:
Using the middleware method, all routes within a group are assigned to the middleware. For example,
Route::middleware(['admin','auther'])->group(function(){
Route::get('/',function(){
});
////
});
Subdomain Routing:
Route groups assign the same PHP namespace to a group of controllers using the namespace method. It may be used to handle subdomain routing.
Route::domain('{account}.myexample.com')->group(function(){
});
Route Prefixes:
The Prefix method is used to prefix each route in the group with a given URI. For example,
Route::prefix('admin')->group(function(){
Route::get('reports',function(){
//given URL is /admin/reports
})
})
Route Name Prefix:
The name method is used to prefix each route in the group with a given string. The ‘.’ character is used to prefix group name and route name. For example,
Route::name('admin')->group(function(){
Route::get('report',function(){
//Route name is admin.report
})->name('report')
})
Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers,