HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers95/rentman.io/app/Http/Middleware/AliasResolver.php
<?php

namespace App\Http\Middleware;

use App\Komma\Routes\Models\RedirectRoute;
use App\Komma\Routes\RouteService;
use App\Komma\Sites\Models\Site;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Http\Request;

final class AliasResolver extends AbstractResolver
{
    /** @var SiteServiceInterface */
    private $siteService;

    /** @var Site */
    private $site;

    public function __construct()
    {
        $this->siteService = \App::make(SiteServiceInterface::class);
        parent::__construct();
    }

    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // Check if middleware could be skipped or is already resolved
        if ($this->isRouteResolved($request) || $this->isKmsRoute($request)) {
            return $next($request);
        }

        $routeService = \App::make(RouteService::class);

        //Check if we can find an activeRoute based on the alias
        /** @var RouteService $routeService */
        if ($route = $routeService->getRouteByAlias($request->path(), $this->siteService->getCurrentSite())) {
            if (is_a($route, RedirectRoute::class)) {
                $regularRoute = ($route->pageTranslation()->first()->route()->first());

                return redirect($regularRoute->alias, $route->redirect_code);
            }

            //Get language of the route
            $language = $route->pageTranslation->language;

            //Check if URL matches the set language else redefine it
            if (isset($language) && \App::getLanguage() != $language) {
                \App::setLanguage($language);
            }

            //Duplicate the request so we can generate an restful Request
            $modifiedRequest = $request->duplicate();

            //Get the routeString form the route
            $route = $route->route;

            //Check if there is an queryString and add this to the route
            //if ($query = $request->getQueryString()) $route . '?' . $query; // Unnecessary because you could still get them by Input::get()

            //Set the request URI and the original path
            $modifiedRequest->server->set('REQUEST_URI', $route);

            //Set the resolved key on the request to tell other route solving things that they don't need to resolve
            $modifiedRequest = $this->setRouteResolved($modifiedRequest, true);

            //Route resolver did resolve route. Pass the request to the next middleware.
            return $next($modifiedRequest);
        }

        //Route resolver could not resolve route. Pass the request to the next middleware.
        return $next($request);
    }
}