- 2 years ago
- Afaq Arif
- 907 Views
-
2
In Laravel, requests are a way to retrieve and validate user input from HTTP requests. They provide a convenient and secure way to handle user input and enforce validation rules before processing the data.
Laravel requests are typically used within controllers to handle the input received from various HTTP methods such as GET
, POST
, PUT
, or DELETE
. Here’s a detailed explanation of Laravel requests with an example:
Creating a Request:
To create a new request in Laravel, you can use the make:request
Artisan command. For example, to create a CreateUserRequest
, run the following command:
php artisan make:request CreateUserRequest
This command will generate a CreateUserRequest
class under the app/Http/Requests
directory. This class extends the Illuminate\Foundation\Http\FormRequest
class.
Defining Validation Rules
Open the generated CreateUserRequest
class and define the validation rules for the incoming request data. The rules()
the method is used to define these rules. For example:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateUserRequest extends FormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users',
'password' => 'required|min:6|confirmed',
];
}
}
In this example, the rules()
the method defines the validation rules for the name
, email
, and password
fields. These rules specify that the name
field is required and should be a string with a maximum length of 255 characters, the email
the field should be a valid email address and unique in the users
table, and the password
field should be required, have a minimum length of 6 characters, and match a confirmation field.
Using Requests in Controllers
To utilize the request in a controller, type-hint the request class in the controller method where you want to access the validated data. For example:
use App\Http\Requests\CreateUserRequest;
public function store(CreateUserRequest $request)
{
// Access validated data using $request object
$name = $request->input('name');
$email = $request->input('email');
$password = $request->input('password');
// Process the data (e.g., create a new user)
// ...
}
In this example, the store
method of the controller receives an instance of the CreateUserRequest
class as an argument. Laravel automatically resolves the request class, validates the incoming data, and passes the validated data to the method.
You can access the validated data using the $request
object, which provides methods like input()
or properties to retrieve the validated data.
Automatic Redirect on Validation Failure
If the validation fails, Laravel automatically redirects the user back to the previous page with the validation errors. You can display these errors in your views to provide feedback to the user. Laravel provides helper methods like withErrors()
to flash the errors to the session and redirect()
to redirect the user back. For example:
public function store(CreateUserRequest $request)
{
// Validation fails automatically redirects back if there are errors
// and flashes the errors to the session
// ...
// If the execution reaches this point, validation has passed
// Process the data (e.g., create a new user)
// ...
return redirect()->route('users.index')->with('success', 'User created successfully.');
}
By utilizing requests in Laravel, you can easily handle validation logic, enforce rules on user input
, and retrieve the validated data in a convenient and secure manner. Requests help in keeping your controller methods clean and focused on the business logic while delegating the validation to dedicated classes.
- 2 years ago
- Afaq Arif
- 907 Views
-
2