Laravel Request

0
208

Retrieval of the Request URI/Path

If you want to retrieve a requested URI then you have to use the ‘path’ method. There is another method called ‘is’. This method works when you want to match the requested URI to the specific argument passed in the method. And, if you want to retrieve the complete URL then you can use the ‘url’ method.

For a better understanding of all these methods, take a look at the following example.

Step 1: Create a new controller under the name of URIController. To create this controller, you need to execute the given command on the command prompt (cmd) or on the terminal.

laravel> php artisan make:controller URIController –plain

Step 2: After the successful creation of the controller, you need to copy the below-mentioned code on the URIController.php file. This file will be available on app/Http/Controllers.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller;

class URIController extends Controller

{

   public function index(Request $request)

{

      // Usage of path method

      $path = $request->path();

      echo ‘Path Method: ‘.$path;

      echo ‘<br>’;

      // Usage of is method

      $pattern = $request->is(‘foo/*’);

      echo ‘is Method: ‘.$pattern;

      echo ‘<br>’;

      // Usage of url method

      $url = $request->url();

      echo ‘URL method: ‘.$url;

   }

}

Step 3: Once you are done with pasting the above code to the given file then add the following line of code to the route.php file. You can find this file in the path app/Http.

Route::get(‘/foo/bar’,’URIController@index’);

Step 4: Now, visit the URL: http://localhost:8000/foo/bar. The output should appear like the following.

Path Method: foo/bar

Is Method: 1

URL method: http://localhost:8000/foo/bar

Retrieval of Input

Laravel provides you the option of retrieving the inputs in a method. It can be done either by ‘get’ method or by ‘post’ method. Both the methods work exactly the same. Well, to retrieve the input values, you can use any of the following ways.

* by the use of input() method

With the input() method, you can retrieve only one input value at a time. In other words, if a form is having a single text field, let’s say ‘username’ then you can use this method to retrieve that input value.

$name = $request->input(‘username’);

* by the use of properties of Request instance

Using this approach, you don’t have to write long command lines to retrieve values from a form. Here’s the example,

$request->username

Also Read: Laravel Routing

Now understand both the approaches with the help of given example.

Step 1: You need to start with the creation of a registration form. The registration form will be having text fields such as name, username, and password. After the creation of the form, save it by the register.php name. You can find this file at resources/views.

<html> 

   <head>

      <title>Form Example</title>

   </head>

   <body>

      <form action = “/user/register” method = “post”>

         <input type = “hidden” name = “_token” value = “<?php echo csrf_token() ?>”>

         <table>

            <tr>

               <td>Name</td>

               <td><input type = “text” name = “name” /></td>

            </tr>

            <tr>

               <td>Username</td>

               <td><input type = “text” name = “username” /></td>

            </tr>         

            <tr>

               <td>Password</td>

               <td><input type = “text” name = “password” /></td>

            </tr>

            <tr>

               <td colspan = “2” align = “center”>

                  <input type = “submit” value = “Register” />

               </td>

            </tr>

         </table>      

      </form>   

   </body>

</html> 

Step 2: Now create a controller with the name UserRegistration.

laravel>php artisan make:controller UserRegistration –plain 

Step 3: Once the creation of the controller is successful, you need to include the following code in the UserRegistration.php file. You can locate this file in app/Http/Controllers. 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use App\Http\Controllers\Controller; 

class UserRegistration extends Controller

{

   public function postRegister(Request $request)

{

      //Retrieve the name input field

      $name = $request->input(‘name’);

      echo ‘Name: ‘.$name;

      echo ‘<br>’;

      

      //Retrieve the username input field

      $username = $request->username;

      echo ‘Username: ‘.$username;

      echo ‘<br>’;

     

      //Retrieve the password input field

      $password = $request->password;

      echo ‘Password: ‘.$password;

   }

}

Step 4: Copy the following code to the routes.php file. You can find this file in app/Http.

Route::get(‘/register’,function()

{

   return view(‘register’);

});

Route::post(‘/user/register’,array(‘uses’=>’UserRegistration@postRegister’));

Step 5: Now, paste the URL: http://localhost:8000/register at the PHP’s web server. You will get the following output after entering the given details.