File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/Http/Middleware/AliasResolver.php
<?php
namespace App\Http\Middleware;
use App\KommaApp\Pages\Models\Page;
use App\KommaApp\Posts\Models\PostTranslation;
use App\KommaApp\Routes\Models\RedirectRoute;
use App\KommaApp\Routes\Models\Route;
use App\KommaApp\Routes\RouteService;
use Illuminate\Http\Request;
class AliasResolver extends AbstractResolver
{
/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
//Skip this when it is an Kms route
//If we split the Route files in non kms and kms routes then we could remove this line
if($request->segment(1) == 'kms') return $next($request);
//Also skip resolving if it already is resolved in another place.
if($this->isRouteResolved($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(), \App::getSite()))
{
if(is_a($route, RedirectRoute::class))
{
$redirectModel = $route->routeable()->first();
if(isset($redirectModel->route) && $redirectModel->route()->count() != 0){
$regularRoute = ($route->routeable()->first()->route()->first());
return redirect($regularRoute->alias, $route->redirect_code);
}
elseif(is_a($redirectModel, PostTranslation::class)){
$blogModel = Page::where('code_name', 'blog')->first();
return redirect($blogModel->translation->route->alias.'/'.$redirectModel->slug, 301);
}
}
//Get language of the route
$language = $route->routeable->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);
}
}