Skip to content

Latest commit

 

History

History
305 lines (196 loc) · 5.69 KB

laravel_middleware_and_requests.md

File metadata and controls

305 lines (196 loc) · 5.69 KB
title author format
Laravel Middleware and Requests
Vladimir Lelicanin - SAE Institute
revealjs
theme title-slide-attributes transition footer margin auto-animate preview-links link-external-newwindow scrollable embed-resources slide-level chalkboard multiplex slide-number incremental logo
default
data-background-image data-background-position data-background-repeat data-background-size
top left
no-repeat
100px
fade
Vladimir Lelicanin - SAE Institute Belgrade
0.2
true
auto
true
true
false
1
true
true
true
false

Laravel Middleware and Requests

  • 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

  • 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;
    }
}

Learn more here


Defining Middleware

  • 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');

Learn more here


Middleware Groups

  • 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,
    ];
}

Learn more here


Requests

  • 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)
    {
        //
    }
}

Learn more here


Request Methods

  • You can access the HTTP verb of a request:
$request->method();
  • Or check if the request is of a certain method:
$request->isMethod('post');

Learn more here


Request Input

  • 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');

Learn more here


Request Files

  • To retrieve files from the request:
$file = $request->file('file_ud');
  • To validate uploaded files:
$is_valid = $request->file('avatar')->isValid();

Learn more here


Request Headers

  • To retrieve a specific header from the request:
$value = $request->header('Content-Type');
  • Or all headers:
$headers = $request->headers->all();

Learn more here


Request Cookies

  • To retrieve a specific cookie from the request:
$value = $request->cookie('ck_name');
  • Or all cookies:
$cookies = $request->cookies->all();

Learn more here


Request Query Parameters

  • To retrieve a specific query parameter from the request:
$value = $request->query('key');
  • Or all query parameters:
$query_params = $request->query();

Learn more here


Request Validation

  • 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',
        ]);
    }
}

Learn more here


Conclusion

  • 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.

References