File: D:/HostingSpaces/SBogers10/veltech.komma.pro/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 RouteService */
private $routeService;
public function __construct()
{
parent::__construct();
$this->routeService = app(RouteService::class);
}
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
// Check if route should be excluded from resolving
if($this->isExcludedFromResolving($request)) return $next($request);
/*
|
| Note:
| We don't check if the request is already resolved.
| Because this resolver should be the first one that possibly can resolved a route.
|
*/
//Check if we can find an activeRoute based on the alias
$route = $this->routeService->getRouteByAlias($request->getPathInfo(), $this->siteService->getCurrentSite());
// If route isn't found, continue request
if(!$route) return $next($request);
// Check if we have a redirect route
if(is_a($route, RedirectRoute::class))
{
$regularRoute = ($route->pageTranslation()->first()->route()->first());
return redirect($regularRoute->alias, $route->redirect_code);
}
// Get the page translation of the found route
$pageTranslation = $route->pageTranslation;
// If route has no page translation, continue request (shouldn't be possible)
if(!$pageTranslation) return $next($request);
//Check if page translation matches the set language else redefine it
$language = $pageTranslation->language;
if(\App::getLanguage() != $language) \App::setLanguage($language);
//Duplicate the request so we can generate an restful Request
$modifiedRequest = $request->duplicate();
//Append page id to request, for grabbing the page when we have changed the rest route
$modifiedRequest->attributes->add(['page_id' => $pageTranslation->page_id]);
//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);
}
}