The complete Lavarel framework development tutorial

Powered by Blogger.

Wednesday 13 July 2016

HTTP Routing- Laravel Routing Tutorial


Hi friends, welcome to the second post HTTP Routing- Laravel Routing Tutorial of this website. If you have installed laravel on your local machine successfully, then we will learn HTTP routing in this Laravel Routing Tutorial. If you are unable to install Laravel on your local machine, then follow the very first tutorial of this website.

Laravel Tutorial for Beginners: Installation

What is HTTP Routing


HTTP Routing is the concept of setting a custom url for a specific route of your web application.

For example- If you type an address of your browser's address bar http://localhost/w3laravel/public/foo (Replace w3laravel with your project name), it will display an error because we don't have this route in our web application.

HTTP Routing: Basic Routing


Laravel routes are defined in routes.php file located in apps -> Http folder of your application folder.

The syntax of defining a basic route:
Route::get('foo', function () {
    echo 'This is the example of basic routing';
});

Copy this code to the file routes.php and go to http://localhost/w3laravel/public/foo (Replace w3laravel with your project name). Now this will not produce an error, but display the text This is the example of basic routing

Different Router methods



  • Route::get($uri, $callback)

  • Route::post($uri, $callback)

  • Route::put($uri, $callback)

  • Route::delete($uri, $callback)

Route Parameters / URL Parameters


Sometimes, it happens that you need to capture a part of your URI to the route. In these cases, we can pass a parameter to our router method.

For example, if you are creating a user login website and you need to print the user id to the route, then you can capture the user id from the URL.

The syntax is like this:
Route::get('foo/{id}', function ($id) {
    //return view('welcome');
    echo 'Id is:' . $id;
});

We can access that route by going to http://localhost/w3laravel/public/foo/101. The 101 is the id of any user. The output will be-

Id is:101

Optional Route Parameters


You can also make the route parameters optional by placing a question mark (?) sign after the parameter name and giving a default value to the route parameter.
Route::get('foo/{id?}', function ($id = null) {
    echo 'Id is:' . $id;
});

Route Parameter Constraints


You can also set constraints to route parameters using the where method.

  • ->where('id', '[A-Za-z]+'): Constrain your parameter to A-Z and a-z.

  • ->where('id', '[0-9]+'): Constrain your parameter to 0-9.

  • ->where(['id' => '[0-9]+', 'name' => '[a-z]+']): Used to constrain multiple parameters.

Syntax for defining a constrain to route parameter:
Route::get('foo/{id}', function ($id) {
    echo 'Id is:' . $id;
})
->where('id', '[0-9]+');

HTTP Routing: Understanding different Route Methods


The different route methods are used for CRUD (Create, Read, Update, Delete) operations.

  • Route::post($uri, $callback): Create an item

  • Route::get($uri, $callback): Read an item

  • Route::put($uri, $callback): Update an item

  • Route::delete($uri, $callback): Delete an item

Route::get($uri, $callback)


We already have seen the example of a get route. It is typically used for reading an item.
Route::get('foo', function () {
    echo 'GET Method';
});


GET- HTTP Routing- Laravel Routing Tutorial- W3Laravel


Route::post($uri, $callback)


To hit the post route, we actually need to create a form inside the get route. In the form the method should be post and the action should be the uri of the post route.
Route::post('foo', function(){
 echo 'POST';
});

Route::get('foo', function(){
 echo '<form method="POST" action="foo">';
 echo '<input type="submit">';
 echo '</form>';
});

Submit- HTTP Routing- Laravel Routing Tutorial- W3Laravel

POST- HTTP Routing- Laravel Routing Tutorial- W3Laravel

Route::put($uri, $callback)


As all the browsers only knows about the put and get method. So to hit the put method we need to add a hidden input to the form like this.
Route::get('foo', function(){
 echo '<form method="POST" action="foo">';
 echo '<input type="hidden" value="PUT" name="_method">';
 echo '<input type="submit">';
 echo '</form>';
});

Route::put('foo', function(){
 echo 'PUT Method';
});

Submit- HTTP Routing- Laravel Routing Tutorial- W3Laravel




PUT- HTTP Routing- Laravel Routing Tutorial- W3Laravel

Route::delete($uri, $callback)


The delete method also need a hidden input, but the value should changed to DELETE.
Route::get('foo', function(){
 echo '<form method="POST" action="foo">';
 echo '<input type="hidden" value="DELETE" name="_method">';
 echo '<input type="submit">';
 echo '</form>';
});


Route::delete('foo', function(){
 echo 'DELETE Method';
});

Submit- HTTP Routing- Laravel Routing Tutorial- W3Laravel




DELETE- HTTP Routing- Laravel Routing Tutorial- W3Laravel


So, here I ends with Laravel Routing Tutorial. In the post HTTP Routing- Laravel Routing Tutorial, I've tried to cover everything related to the topic. If you have any queries related to this post, getting any error or want to suggest something regarding this post, feel free to comment or contact using contact us form.

0 comments:

Post a Comment