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:
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:
Step 4:
Add the following route to routes/web.php
(not app/Http/routes.php
anymore in the latest Laravel versions):
Step 5:
Visit:
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:
Using Request Properties
Alternatively, you can directly access input values as properties:
Example: Handling User Registration
Step 1:
Create a registration form at resources/views/register.blade.php
:
Note: Use Blade syntax (@csrf
) instead of manually inserting csrf_token()
.
Step 2:
Create a controller for handling registration:
Step 3:
Open app/Http/Controllers/UserRegistration.php
and update it:
Step 4:
Add the following routes in routes/web.php
:
Step 5:
Visit:
Please complete and submit the form. The registration details will be printed on the next page.