title | author | format | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Laravel Middleware and Requests |
Vladimir Lelicanin - SAE Institute |
|
- Middleware provides a way to filter HTTP requests entering your application.
- Requests encapsulate HTTP methods (GET, POST, DELETE, PATCH, PUT, etc.) and give you tools to work with them.
- Middleware functions are invoked before a request is sent to the server.
- They can be used for tasks like user authentication, input validation, or logging.
<?php
namespace App\Http\Middleware;
use Closure;
class ExampleMiddleware
{
public function handle($request, Closure $next)
{
// Perform action before request sends
$response = $next($request); // Call the next middleware
// Perform action after response returns
return $response;
}
}
- Middleware can be defined globally, on a route, or on a controller.
<?php
// Global middleware
protected $middleware = [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
];
// Route middleware
Route::get('/', function () {
//
})->middleware('example');
- Middleware groups allow you to group several middleware under a single key to simplify routes and avoid repetition.
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TransformsRequest;
class ExampleMiddlewareGroup
{
protected $middleware = [
TransformsRequest::class,
];
}
Illuminate\Http\Request
represents an HTTP request.- The request carries HTTP headers, cookies, and body content.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function exampleAction(Request $request)
{
//
}
}
- You can access the HTTP verb of a request:
$request->method();
- Or check if the request is of a certain method:
$request->isMethod('post');
- To retrieve a single value from the request input, you can use the get method:
$value = $request->get('key');
- Or access input values by name:
$name = $request->input('user.name');
- To retrieve files from the request:
$file = $request->file('file_ud');
- To validate uploaded files:
$is_valid = $request->file('avatar')->isValid();
- To retrieve a specific header from the request:
$value = $request->header('Content-Type');
- Or all headers:
$headers = $request->headers->all();
- To retrieve a specific cookie from the request:
$value = $request->cookie('ck_name');
- Or all cookies:
$cookies = $request->cookies->all();
- To retrieve a specific query parameter from the request:
$value = $request->query('key');
- Or all query parameters:
$query_params = $request->query();
- Laravel provides a convenient way to validate incoming HTTP request data.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
public function exampleAction(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'body' => 'required|string',
]);
}
}
- Middleware provides a way to filter HTTP requests.
- Requests encapsulate HTTP methods and provide tools to work with them.
- Use Laravel's built-in features to filter & validate incoming requests.