Laravel Routing Techniques

Laravel tinker functionality
June 23, 2017

Laravel Routing Techniques

Before you can visit any page or post of your laravel application you will have to specifies the route for individual static page or dynamic post.

Laravel determines the way to control the the contents by assigning them to a controller.Each Controller in laravel consist of different function that controls the various data on the type of methods such as-:

  1. GET  Request  => To Show Data
  2. POST  Request=>To Store The data Inside The Database
  3. PUT Request    => To Update The Data
  4. DELETE Request=>To delete the Data

In order to Pass the requested data to the specified controller we will have to define what is called Routes

Basic Routing-:

The simplest route that can be define is:

Route::get(‘/home’,function(){

return view(‘welcome’);

});

In above route ‘/home’ is the url that appears to user something like “http://www.yourdomain.com/home”

Route::get() is defined for the GET method similarly we can define something like Route::post(),Route::put(),Route::delete() for POST, PUT and DELETE request

The Above route says that when user visits the url ” http://www.yourdomain.com/home” return the content inside the welcome.blade.php

If you want to to go to controller through route you can define something like

Route::get(‘/home’,pagecontroller@index);

where “pagecontroller” is the name of controller and “index” is the method defined inside the pagecontroller

 

Resource Routing

Suppose that you are working on big project which consists of hundreds of form that it might become a heavy task to define individual route for the CRUD(Create-Read-Update-Delete) functionality.

Resource routes comes in handy when you need to define the controller that handles the Crud functionality.

To generate resourceful controller : php artisan make:controller –resource;

To define the route for resource controller: Route::resource(‘form’,’formController’);

The above resource would define the List of following individual route which you would have probably defined if working individually:

Route::get(‘form’,’formController@index’);

Route::get(‘form/create’,’formController@create’);

Route::post(‘form/store’,’formController@store’);

Route::get(‘form/{id}/edit’,’formController@edit’);

Route::put(‘form/{id}’,’formController@update’);

Route::delete(‘form/{id}’,’formController@destroy’);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

two × 2 =