File: D:/HostingSpaces/SBogers10/vebon.komma.pro/app/KommaApp/Core/Middleware/BeforeMiddleware.php
<?php
namespace KommaApp\Core\Middleware;
use Closure;
use Illuminate\Http\Request;
use KommaApp\Routes\RouteService;
class BeforeMiddleware
{
/**
* In this method, that is fired before the request.
* We are going to check if there is an db alias.
* If true, we will fire a modified request.
* If not, we check for an alternative route
* and redirect with a 301
*
* @param $request
* @param Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
//redirect if not starts with http
// if( ! starts_with($request->url(), 'https://')){
// return \Redirect::to(str_replace(['http://'], ['https://'], $request->url()), 301);
// }
//Skip this when it is an Kms route
if ($request->segment(1) == 'kms') return $next($request);
$routeService = \App::make(RouteService::class);
//Check if we can find an activeRoute based on the alias
if ($route = $routeService->getActiveRouteByAlias($request->path())) {
//Yes
//Set the site based on the route
\Komma::setSite($route->site);
//Set the locale based on the route
$this->setLanguage($route->language);
//Duplicate the 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;
//Set the request URI`
$modifiedRequest->server->set('REQUEST_URI', $route);
//Return the modifiedRequest
return $next($modifiedRequest);
}
//(No)
//Check if we can find an alternative Route by alias
if ($route = $routeService->getAlternativeRouteByAlias($request->path())) {
//Yes: Redirect with permanently moved
return \Redirect::to($route->alias, 301);
}
//(No)
//Set the site based on the route
\Komma::setSite(null);
$this->setLanguage(\App::getlocale());
//Return the Request, and check the other routes
return $next($request);
}
/**
* This method will set the language
*
* @param $langauge
* @return boolean
*/
private function setLanguage($langauge)
{
\Komma::setLocale($langauge);
}
}