File: D:/HostingSpaces/SBogers85/equichecker.com/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)
{
//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();
//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();
//Return the Request, and check the other routes
return $next($request);
}
/**
* This method will set the language
* It will check if there is an customer
* If so, use this language
* Else use the given $language
*
* @param $langauge
* @return boolean
*/
private function setLanguage()
{
//Check if there is a customer, if true set customer language/
if ($customer = \Auth::customer()->get()) \Komma::setLocale($customer->language_id);
}
}