master

laravel/framework

Last updated at: 29/12/2023 09:25

Middleware.php

TLDR

This file defines the Middleware class in the Illuminate\Routing\Controllers namespace. The class represents a controller middleware and contains properties and methods to specify the middleware behavior.

Classes

Middleware

The Middleware class represents a controller middleware definition. It has the following properties:

  • middleware: The middleware that should be assigned. It can be a closure, string, or an array.
  • only: The controller methods that the middleware should only apply to. It is an array or null.
  • except: The controller methods that the middleware should not apply to. It is an array or null.

The Middleware class has the following methods:

  • __construct: Creates a new controller middleware definition. Accepts the middleware parameter.
  • only: Specifies the only controller methods the middleware should apply to. Accepts an array or a string representing the method names.
  • except: Specifies the controller methods the middleware should not apply to. Accepts an array or a string representing the method names.
<?php

namespace Illuminate\Routing\Controllers;

use Closure;
use Illuminate\Support\Arr;

class Middleware
{
    /**
     * The middleware that should be assigned.
     *
     * @var \Closure|string|array
     */
    public $middleware;

    /**
     * The controller methods the middleware should only apply to.
     *
     * @var array|null
     */
    public $only;

    /**
     * The controller methods the middleware should not apply to.
     *
     * @var array|null
     */
    public $except;

    /**
     * Create a new controller middleware definition.
     *
     * @param  \Closure|string|array  $middleware
     * @return void
     */
    public function __construct(Closure|string|array $middleware)
    {
        $this->middleware = $middleware;
    }

    /**
     * Specify the only controller methods the middleware should apply to.
     *
     * @param  array|string  $only
     * @return $this
     */
    public function only(array|string $only)
    {
        $this->only = Arr::wrap($only);

        return $this;
    }

    /**
     * Specify the controller methods the middleware should not apply to.
     *
     * @param  array|string  $except
     * @return $this
     */
    public function except(array|string $except)
    {
        $this->except = Arr::wrap($except);

        return $this;
    }
}