master

laravel/framework

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

ControllerDispatcher.php

TLDR

This file, ControllerDispatcher.php, is part of the Illuminate\Routing namespace in the Demo Projects project. It contains a class called ControllerDispatcher that implements the ControllerDispatcherContract interface. The class is responsible for dispatching a request to a controller and method, as well as resolving parameters and middleware for the controller.

Methods

dispatch

This method receives a Route instance, a controller, and a method as parameters. It resolves the parameters for the controller and then dispatches the request by calling the appropriate method on the controller.

resolveParameters

This method receives a Route instance, a controller, and a method as parameters. It resolves the parameters for the controller by using the resolveClassMethodDependencies method and returns an array of resolved parameters.

getMiddleware

This method receives a controller and a method as parameters. It retrieves the middleware for the controller instance by calling the getMiddleware method on the controller. It filters the middleware based on the options provided and returns an array of middleware.

Classes

There are no additional classes in this file.

<?php

namespace Illuminate\Routing;

use Illuminate\Container\Container;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;

class ControllerDispatcher implements ControllerDispatcherContract
{
    use FiltersControllerMiddleware, ResolvesRouteDependencies;

    /**
     * The container instance.
     *
     * @var \Illuminate\Container\Container
     */
    protected $container;

    /**
     * Create a new controller dispatcher instance.
     *
     * @param  \Illuminate\Container\Container  $container
     * @return void
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
    }

    /**
     * Dispatch a request to a given controller and method.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  mixed  $controller
     * @param  string  $method
     * @return mixed
     */
    public function dispatch(Route $route, $controller, $method)
    {
        $parameters = $this->resolveParameters($route, $controller, $method);

        if (method_exists($controller, 'callAction')) {
            return $controller->callAction($method, $parameters);
        }

        return $controller->{$method}(...array_values($parameters));
    }

    /**
     * Resolve the parameters for the controller.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  mixed  $controller
     * @param  string  $method
     * @return array
     */
    protected function resolveParameters(Route $route, $controller, $method)
    {
        return $this->resolveClassMethodDependencies(
            $route->parametersWithoutNulls(), $controller, $method
        );
    }

    /**
     * Get the middleware for the controller instance.
     *
     * @param  \Illuminate\Routing\Controller  $controller
     * @param  string  $method
     * @return array
     */
    public function getMiddleware($controller, $method)
    {
        if (! method_exists($controller, 'getMiddleware')) {
            return [];
        }

        return collect($controller->getMiddleware())->reject(function ($data) use ($method) {
            return static::methodExcludedByOptions($method, $data['options']);
        })->pluck('middleware')->all();
    }
}