master

laravel/framework

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

SubstituteBindings.php

TLDR

This file, SubstituteBindings.php, is part of the Illuminate\Routing\Middleware namespace and contains the SubstituteBindings class. This class handles the substitution of route bindings in an incoming request.

Classes

SubstituteBindings

The SubstituteBindings class is a bindings substitutor. It implements the handle method which is responsible for substituting route bindings in an incoming request. This class also has a constructor method to inject the Router instance.

Methods

None

<?php

namespace Illuminate\Routing\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Registrar;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class SubstituteBindings
{
    /**
     * The router instance.
     *
     * @var \Illuminate\Contracts\Routing\Registrar
     */
    protected $router;

    /**
     * Create a new bindings substitutor.
     *
     * @param  \Illuminate\Contracts\Routing\Registrar  $router
     * @return void
     */
    public function __construct(Registrar $router)
    {
        $this->router = $router;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        try {
            $this->router->substituteBindings($route = $request->route());

            $this->router->substituteImplicitBindings($route);
        } catch (ModelNotFoundException $exception) {
            if ($route->getMissing()) {
                return $route->getMissing()($request, $exception);
            }

            throw $exception;
        }

        return $next($request);
    }
}