Laravel Routing Techiques

Routes are used to access different applicatiion interfaces using http/https reuests. Laravel provide convinient way to manage these http requests.Laravel provide diffeerent files to manage web interface routes and API routes/calls. These can be managed independently using the following route files.

  • routes/web.php
  • routes/api.php

Types of Route

 Most commonly web.php is used to define routes for web interfaces. Laravel router allows to register routes for responding to following HTTP requests.

  1. Route::get(URI, Callback);
  2. Route::post(URI, Callback);
  3. Route::delete(URI, Callback);
  4. Route::put(URI, Callback);
  5. Route::patch(URI, Callback);
  6. Route::options(URI, Callback);

1. Get Route

Get HTTP request is normally used to load web interfaces. The parameters of request will displayed in URL.

Examples

Simple GET route.

Route::get('/URI', 'AppController@loadView');

 Named GET route.

Route::get('/URI', ['as' => 'loadURI', 'uses' => 'AppController@LoadInterface']);

 GET route with callback function.

Route::get('/welcome', function()
{
    return "Institute Management System";
});

2. Post Route

Post HTTP request is used to send data to particular URI. In Laravel any form that pointing to POST route must have CSRF token. If request does not contain CSRF token then POST request will be denied.

Examples

Route::post('/URI', 'AppController@saveData');
Route::post('/URI', ['as' => 'saveURI', 'uses' => 'AppController@saveData']);

Laravel Dynamic Routes

There may be situation when we waant to register single route for more than one HTTP verbs. To handle this situation Laravel provide dynamic handling of http requests.

  1. Route::match(URI, Callback);
  2. Route::any(URI, Callback);
  3. Fallback Route

1. Match Route

Match route match the specific HTTP request verb within available methods. These methods are provided as an array in route.

Examples

Route::match(['get','post'], '/URI', 'AppController@controllerMethod');
Route::match(['get','post'],'/list',function() {

// code will be here.

});

2. Any Route

Laravel Any route is used to repond every HTTP verbs for specific URI. This is helpful when there are multiple URI having different HTTP verbs.

Example

Route::any('/URI' , function() {

// code will goes here

});

3. Fallback Route

Laravel fallback route is used when no other route exist to handle HTTP request. Normally these request automatically goes to 404 page through Laverel exception handling.

Example

Route::fallback(function() {

// code goes here

});

Laravel Specific Routes

  1. View Routes
  2. Redirect Routes
  3. Named Routes
  4. Group Routes
  5. Prefix Routes
  6. Resource Route

 

 

Comments
Login to TRACK of Comments.