Concept of Access Request in Laravel Tutorial

In this tutorial, we are going to learn about request in Laravel.

Retrieving the Request URI

Don't Miss

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the actual sample specified within the argument of the method. To get the full URL, we are able to use the url method.

Step 1: Open the terminal and execute the below command:

php artisan make:controller UriController

Step 2: After making a controller, add the next code within the app/Http/Controllers/UriController.php file.

<?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: Add the next line within the routes/web.php file.

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

Step 4: Visit the below URL.

http://YOUR_PROJECT_DOMAIN/foo/bar

Retrieving Input

The input values can be easily retrieved in Laravel. No matter what method was used “get” or “post”, the Laravel method will retrieve input values for both the methods the same way. There are two ways we can retrieve the input values.

  • Using the input() method
  • Using the properties of Request instance

Using the input() method

The input() method takes one argument, the name of the field within the form. For example, if the form contains username area then we are able to access it within the following method.

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

Using the properties of Request instance

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

$request->username

Below we’re offering a easy example wherein we are going to create a views for user registration. Creating a UserRegistration controller & define route within the web.php

Step 1 − Creating a Registration form, where a user can register and created register.php file in the resources/views/ directory

<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 following command to create a UserRegistration controller.

php artisan make:controller UserRegistration

Step 3 – Replace the following code in app/Http/Controllers/UserRegistration.php controller.

<?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 – Add the below line in routes/web.php file.

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

Visit the below URL and you will see the a form. Enter the registration details and click Register and you will see on the second page that we have retrieved and displayed the user registration details.

http://YOUR_PROJECT_DOMAIN/register

<— Previous Tutorial Next Tutorial —>

LEAVE A REPLY

Please enter your comment!
Please enter your name here

564FansLike

Recent Posts

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.

Related Articles

Concept of Session in Laravel Tutorial

Sessions are used to store details about the user throughout the requests. Laravel supplies various drivers like file, cookie, apc, array, Memcached, Redis, and database to handle session data. By default, file driver is used as a result of it's light-weight....

Laravel Url Generation Tutorial

Our web application revolves around routes and URLs. After all, they're what direct our users to our pages. At the end of the day, serving pages is what any web application should do. Our users may...

Concept of Laravel Views Tutorial

In MVC framework, the letter "V" stands for Views. It separates the application logic and presentation logic. Views are saved in resources/views listing. Generally, the view contains the HTML which might be served by the application.
WP2Social Auto Publish Powered By : XYZScripts.com