Laravel - Request

Laravel - Request

Working with Requests in Laravel

This chapter teaches you how to handle and retrieve data from HTTP Requests in Laravel.

Retrieving the Request URI

Laravel provides several methods to retrieve information about the request URL:

  • path() — Get the requested URI path.

  • is() — Check if the request URI matches a given pattern.

  • url() — Get the full URL.

Example: Retrieving URI Information

Step 1:
Create a new controller named UriController by running:

php artisan make:controller UriController --plain

Step 2:
Once the command is successful, you’ll have a new controller: UriController.

Step 3:
Open app/Http/Controllers/UriController.php and update it as follows:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class UriController extends Controller { public function index(Request $request) { // Retrieve the request path $path = $request->path(); echo 'Path Method: ' . $path . '<br>'; // Check if path matches 'foo/*' $pattern = $request->is('foo/*'); echo 'Is Method: ' . ($pattern ? 'true' : 'false') . '<br>'; // Retrieve the full URL $url = $request->url(); echo 'URL Method: ' . $url; } }

Step 4:
Add the following route to routes/web.php (not app/Http/routes.php anymore in the latest Laravel versions):

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

Step 5:
Visit:

http://localhost:8000/foo/bar

Step 6:
You’ll see an output displaying the path, pattern match result, and full URL.

Retrieving Input Data

Laravel makes it easy to retrieve user input regardless of whether the request method is GET or POST.

You can retrieve input values in two ways:

  • Using the input() method.

  • Accessing request properties directly.

Using the input() Method

You can get input values like this:

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

Using Request Properties

Alternatively, you can directly access input values as properties:

$username = $request->username;

Example: Handling User Registration

Step 1:
Create a registration form at resources/views/register.blade.php:

<html> <head> <title>Form Example</title> </head> <body> <form action="/user/register" method="post"> @csrf <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>

Note: Use Blade syntax (@csrf) instead of manually inserting csrf_token().

Step 2:
Create a controller for handling registration:

php artisan make:controller UserRegistration --plain

Step 3:
Open app/Http/Controllers/UserRegistration.php and update it:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class UserRegistration extends Controller { public function postRegister(Request $request) { $name = $request->input('name'); echo 'Name: ' . $name . '<br>'; $username = $request->username; echo 'Username: ' . $username . '<br>'; $password = $request->password; echo 'Password: ' . $password; } }

Step 4:
Add the following routes in routes/web.php:

Route::get('/register', function () { return view('register'); }); Route::post('/user/register', [UserRegistration::class, 'postRegister']);

Step 5:
Visit:

http://localhost:8000/register

Please complete and submit the form. The registration details will be printed on the next page.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

3 Comments

CAN FEEDBACK
  1. Anonymous
    Anonymous
    dd
  2. Anonymous
    Anonymous
    dd
  3. Anonymous
    Anonymous
    hihi
close