master

laravel/framework

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

Controller.php

TLDR

This file defines an abstract Controller class that can be extended by other classes in the Illuminate\Routing namespace. The Controller class includes methods for registering middleware on the controller, getting the middleware assigned to the controller, executing an action on the controller, and handling calls to missing methods on the controller.

Methods

middleware

This method is used to register middleware on the controller. It accepts a middleware or an array of middleware as the first parameter and an optional array of options as the second parameter. The method adds the middleware to the middleware property of the controller and returns an instance of the ControllerMiddlewareOptions class.

getMiddleware

This method returns the middleware assigned to the controller as an array.

callAction

This method is used to execute an action on the controller. It accepts the method name as the first parameter and an array of parameters as the second parameter. The method dynamically calls the specified method on the controller with the provided parameters and returns the result.

__call

This method handles calls to missing methods on the controller. It is triggered when a method that does not exist on the controller is called. The method throws a BadMethodCallException with an error message indicating the method and class name that do not exist.

<?php

namespace Illuminate\Routing;

use BadMethodCallException;

abstract class Controller
{
    /**
     * The middleware registered on the controller.
     *
     * @var array
     */
    protected $middleware = [];

    /**
     * Register middleware on the controller.
     *
     * @param  \Closure|array|string  $middleware
     * @param  array  $options
     * @return \Illuminate\Routing\ControllerMiddlewareOptions
     */
    public function middleware($middleware, array $options = [])
    {
        foreach ((array) $middleware as $m) {
            $this->middleware[] = [
                'middleware' => $m,
                'options' => &$options,
            ];
        }

        return new ControllerMiddlewareOptions($options);
    }

    /**
     * Get the middleware assigned to the controller.
     *
     * @return array
     */
    public function getMiddleware()
    {
        return $this->middleware;
    }

    /**
     * Execute an action on the controller.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters)
    {
        return $this->{$method}(...array_values($parameters));
    }

    /**
     * Handle calls to missing methods on the controller.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws \BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        throw new BadMethodCallException(sprintf(
            'Method %s::%s does not exist.', static::class, $method
        ));
    }
}