master

laravel/framework

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

ControllerMiddlewareOptions.php

TLDR

This file contains the ControllerMiddlewareOptions class, which provides methods for setting middleware options for controller methods.

Methods

only

This method sets the controller methods that the middleware should apply to. It accepts an array, string, or a variable list of method names as arguments. Returns an instance of the class.

except

This method sets the controller methods that the middleware should exclude. It accepts an array, string, or a variable list of method names as arguments. Returns an instance of the class.

<?php

namespace Illuminate\Routing;

class ControllerMiddlewareOptions
{
    /**
     * The middleware options.
     *
     * @var array
     */
    protected $options;

    /**
     * Create a new middleware option instance.
     *
     * @param  array  $options
     * @return void
     */
    public function __construct(array &$options)
    {
        $this->options = &$options;
    }

    /**
     * Set the controller methods the middleware should apply to.
     *
     * @param  array|string|mixed  $methods
     * @return $this
     */
    public function only($methods)
    {
        $this->options['only'] = is_array($methods) ? $methods : func_get_args();

        return $this;
    }

    /**
     * Set the controller methods the middleware should exclude.
     *
     * @param  array|string|mixed  $methods
     * @return $this
     */
    public function except($methods)
    {
        $this->options['except'] = is_array($methods) ? $methods : func_get_args();

        return $this;
    }
}