master

laravel/framework

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

HostValidator.php

TLDR

This file contains the HostValidator class, which implements the ValidatorInterface. Its main method is matches, which validates a given rule against a route and request.

Classes

HostValidator

The HostValidator class is responsible for validating a given rule against a route and request. It implements the ValidatorInterface. The matches method validates the host of the request against the host regex of the route.

<?php

namespace Illuminate\Routing\Matching;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;

class HostValidator implements ValidatorInterface
{
    /**
     * Validate a given rule against a route and request.
     *
     * @param  \Illuminate\Routing\Route  $route
     * @param  \Illuminate\Http\Request  $request
     * @return bool
     */
    public function matches(Route $route, Request $request)
    {
        $hostRegex = $route->getCompiled()->getHostRegex();

        if (is_null($hostRegex)) {
            return true;
        }

        return preg_match($hostRegex, $request->getHost());
    }
}