Laravel - Request

Laravel - Request

Getting the request URI

The "path" method is used to get the requested URI. The is method is used to get the requested URI that matches the specific pattern specified in the method argument. To get the full URL we can use the url method .

example

Step 1 − Run the below command to create a new controller named UriController .

php artisan make:controller UriController --plain

Step 2. After successfully executing the URL, you will get the following output:

Step 3 − After creating the controller, add the following code to this file.

app/Http/Controllers/UriController.php

<? 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 4 − Add the following line to app/Http/route.php file .

app/http/route.php

Route::get('/foo/bar','UriController@index');

Step 5 − Visit the following URL.

http://localhost:8000/foo/bar

Step 6 − The output will look as shown in the following image.

Getting input

Input values ​​can be easily received in Laravel. Regardless of which "get" or "post" method was used , the Laravel method will fetch the input values ​​for both methods in the same way. There are two ways to get input values.

  • Using the input() method
  • Using Request Instance Properties

Using the input() method

The input() method takes one argument, the name of the field in the form. For example, if a form contains a username field, we can access it like this.

$name = $request->input('username');

Using Request Instance Properties

Like the input() method , we can get the username property directly from the request instance.

$request->username

example

Take a look at the following example to better understand queries.

Step 1 - Create a registration form where the user can register and save the form in resources/views/register.php

<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 − Run the below command to create the UserRegistration controller .

php artisan make:controller UserRegistration --plain

Step 3 − After successfully completing the above step, you will get the following output −

Step 4 - Copy the following code into

app/Http/Controllers/UserRegistration.php controller.

app/Http/Controllers/UserRegistration.php

<?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 5 − Add the following line to app/Http/rout.php file .

app/http/routes.php

Route::get('/register',function() {
   return view('register');
});
Route::post('/user/register',array('uses'=>'UserRegistration@postRegister'));

Step 6 − Visit the following URL and you will see the registration form as shown in the image below. Enter the registration details and click "Register" and you will see on the second page we got and displayed the user's registration details.

http://localhost:8000/register

Step 7 − The output will look something like below in the following images.