Friday, 10 April 2015

Get and Post request in laravel

This post is about handling GET and POST request in Laravel.But before we start you should be familiar with Routing in Laravel.Because routing is the most fundamental part in laravel.Since this is a step by step series I suggest you to read amy previous post if you find anything confusing....So let's get started.
To understand GET and POST request in laravel we will be creating a simple form with Name and Password field.We will submit this form using both GET and POST method so that things become clear :)
Before we start building our form we must create a route to our form.Open routes.php and create the following route


1
2
3
Route::get('form',function(){
 return View::make('form');
});

We have created a simple route without any parameters to form.blade.php view which holds our form
Create form.blade.php and create a  simple form




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form method="get" action="verify_form">
 <label>Name</label>
 <input type="text" name="uname">
 <br>
 <label>Password</label>
 <input type="password" name="pass">
 <br>
 <input type="submit">
</form>
</body>
</html>

Simple HTML form with method as get and action set to verify_form.If you try to submit the form now you will get errors since we have not create a route to verify_form view.Open routes.php and create a route to verify_form.blade.php



1
2
3
Route::get('verify_form',function(){
 return View::make('verify_form');
});

Now since we have created a route to verify_form view we will just verify if our form is working by just printing the variable using $_POST[] method of php.
Open verify_form file and type in the following 2 lines




1
2
Name: {{$_GET['uname']}}<br>
Password: {{$_GET['pass']}}

Output
So our routes and all other methods are working properly...Remember GET is not the best method when you are dealing with sensitive data as you can see that uname and pass variable can be seen in the URL of the browser.
Coming to POST method the code almost remains the same except few changes like form method becomes post,Instead of get route we will create post route and we will grab the variable in verify_form using $_POST[] method
form.blade.php file




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<form method="post" action="verify_form">
 <label>Name</label>
 <input type="text" name="uname">
 <br>
 <label>Password</label>
 <input type="password" name="pass">
 <br>
 <input type="submit">
</form>
</body>
</html>

routes.php file


1
2
3
Route::post('verify_form',function(){
 return View::make('verify_form');
});

verify_form file


1
2
Name: {{$_POST['uname']}}<br>
Password: {{$_POST['pass']}}

Remember the first route remains same in both the cases..and so the output.

2 comments:

  1. Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user's ID from the URL. You may do so by defining route parameters:

    Route::get('user/{id}', function ($id) {
    return 'User '.$id;
    });

    Freelance Laravel Developers , hire at $4.95 at GeeksPerHour.

    ReplyDelete