How to create a Resourceful Routing in Laravel 5.2
In this post we will give you information about How to create a Resourceful Routing in Laravel 5.2. Hear we will give you detail about How to create a Resourceful Routing in Laravel 5.2And how to use it also give you demo for it if it is necessary.
How to create a Resourceful Routing in Laravel 5.2
Before going to know about route resource, you should know about basic routes in Laravel. Normally you define your routes for your application in the app/Http/routes.php
file but did you know how routes.php
file is loaded.
All routes are loaded by AppProvidersRouteServiceProvider
class.
Laravel resourceful routes are expressed by URI and a Closure (Closure are a technique for defining logically scoped name binding in language with first class functions) :
- Route::get('/',function(){
- return'Welcome to onlinecode';
- });
- Route::post('tutorial/php',function(){
- return'Welcome to onlinecode';
- });
- Route::put('tutorial/php',function(){
- //
- });
- Route::delete('tutorial/php',function(){
- //
- });
Route::get('/', function () { return 'Welcome to onlinecode';});Route::post('tutorial/php', function () { return 'Welcome to onlinecode';});Route::put('tutorial/php', function () { //});Route::delete('tutorial/php', function () { //});
If you want that your route will work with every HTTP verbs then you may use any
methods.
- Route::any('tutorial',function(){
- return'Welcome to Expert PHP';
- });
Route::any('tutorial', function () { return 'Welcome to Expert PHP';});
Now question is how you will generate URLs for application route, see the syntax to generate URLs using url
helper :
$url = url('foo');
Optional or Required Route Parameters :
Sometime you send your http request with needed parameters which you think, it should be required or optional that means segments of URI can be either required or optional so how can you fetch segments or URI and how can you define required or optional segments of URI in your routes :
Required Parameters
- Route::get('product/{id}',function($id){
- return'Product '.$id;
- });
Route::get('product/{id}', function ($id) { return 'Product '.$id;});
You can also define many required parameters in your route :
- Route::get('user/{parent_id}/{child_id}',function($parent_id,$child_id){
- //
- });
Route::get('user/{parent_id}/{child_id}', function ($parent_id, $child_id) { //});
Required Parameters are always define with ‘curly’ braces.
Optional Parameters
Optional Parameters are defined ‘curly’ braces along with ‘?’ mark.
- Route::get('product/{name?}',function($name= null){
- return$name;
- });
- Route::get('product/{name?}',function($name='Expert PHP'){
- return$name;
- });
Route::get('product/{name?}', function ($name = null) { return $name;});Route::get('product/{name?}', function ($name = 'Expert PHP') { return $name;});
Named Routes
You can define your named routes using the as
array keys while defining the route.Benifits with named routes are: you won’t have to care about URLs and you can redired for a specific routes using named routes.
- Route::get('product/list',['as'=>'product',function(){
- //
- }]);
Route::get('product/list', ['as' => 'product', function () { //}]);
You can also define named routes for your controller actions.
- Route::get('product/list',[
- 'as'=>'product','uses'=>'ProductController@listProduct'
- ]);
Route::get('product/list', [ 'as' => 'product', 'uses' => 'ProductController@listProduct']);
Now Question is how you will access named routes in your controller, view or anywhere in application.
Accessing Named Routes
- $url=route('product');
- $redirect=redirect()->route('product');
$url = route('product');$redirect = redirect()->route('product');
Route Prefixes
You can also define prefix for every routes in the group by using prefix
group array attribute.
Suppose you are creating a admin module in your application and you need to set prefix with name admin then go through with following syntax :
- Route::group(['prefix'=>'admin'],function(){
- Route::get('products',function(){
- // Matches The "/admin/products" URL
- });
- });
Route::group(['prefix' => 'admin'], function () { Route::get('products', function () { // Matches The "/admin/products" URL });});
Defining PHP Namespace to group of Controllers
You can use namespace
key to define namespaces for all of your controllers within group.
- Route::group(['namespace'=>'Admin'],function()
- {
- // Controllers Within The "AppHttpControllersAdmin" Namespace
- });
Route::group(['namespace' => 'Admin'], function(){ // Controllers Within The "AppHttpControllersAdmin" Namespace});
If you want to filter your routes then you will have to define middleware key for all routes which you want to set within filters.
For example, suppose you want to access some url after authentication then go through with following code :
- Route::group(['middleware'=>'auth'],function(){
- Route::get('user/dashboard',function(){
- // Uses Auth Middleware
- });
- });
Route::group(['middleware' => 'auth'], function () { Route::get('user/dashboard', function () { // Uses Auth Middleware });});
Resource Routes
Route::resource('user', 'UserController');
Awesome features comes with Laravel is resourceful routing which have following methods your resource controller.
Action | Route Name |
---|---|
index | user.index |
create | user.create |
store | user.store |
show | user.show |
edit | user.edit |
update | user.update |
destroy | user.destroy |
Hope this code and post will helped you for implement How to create a Resourceful Routing in Laravel 5.2. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs