File: D:/HostingSpaces/bomacon/bomacon.nl/app/Http/Middleware/LocalizedRouteResolver.php
<?php
namespace App\Http\Middleware;
use App\Routes\Models\Route;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Komma\KMS\Globalization\Languages\Models\Language;
/**
* Class LocalizedRoute
*
* Does have a look at the first segment from the route and looks that up in the iso_2 column of the language table.
* If it matches a language. It uses that language to set the app's language. It then strips the first segment from the url.
* After that, all the values from the translations from site/routes.php will be compared against the url.
* If it matches, the corresponding key is the named route where the user will be sent to.
*/
final class LocalizedRouteResolver extends AbstractResolver
{
/**
* @param \Illuminate\Http\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);
// Check if route is already resolved
if($this->isRouteResolved($request)) return $next($request);
$this->log('Trying to resolve: '.$request->path());
$segments = $request->segments();
if(config('app.multipleLanguages')) {
//TODO rewrite this so that only the current site available languages should be checked
$possibleLanguage = Language::where('iso_2', '=', $request->segments()[0])->first();
//Change the app language to the one from the url
if ($possibleLanguage && \App::getLanguage() != $possibleLanguage) {
$this->log('Changed app language to the one listed in the url: '.$possibleLanguage->iso_2);
\App::setLanguage($possibleLanguage);
}
//Remove the language iso 2 from the request
array_shift($segments);
}
//Check if the remaining route uri matches named route in the site routes translation file. If so, Change the url to the original named url and return that.
$newRoute = $this->getResolvedNamedRouteFromTranslatedRoute(implode('/', $segments));
if($newRoute) {
$translatedModifiedRequest = $request->duplicate();
$translatedModifiedRequest->server->set('REQUEST_URI', $newRoute);
$this->setRouteResolved($translatedModifiedRequest);
return $next($translatedModifiedRequest);
}
//Just return the url without the iso_2 language prefix.
return $next($request);
}
/**
* @param string $translatedRouteCandidate
* @return string
*/
private function getResolvedNamedRouteFromTranslatedRoute(string $translatedRouteCandidate):string
{
//Example $translatedRouteCandidate = passwort/zurucksetzen/123As4$as4125sdasjfisghvcda$2512
//Example 'site.password.reset passwort/zurucksetzen
$translationRoutes = __('site/routes');
//Sort by length of translated routes routes
uasort($translationRoutes, function ($routeTranslationA, $routeTranslationB) {
return strlen($routeTranslationB) - strlen($routeTranslationA);
});
foreach ($translationRoutes as $namedRoute => $translationRoute)
{
$length = mb_strlen($translationRoute);
$translatedRouteCandidateTrimmed = substr($translatedRouteCandidate, 0, $length); //Example: passwort/zurucksetzen
$parameters = explode('/', substr($translatedRouteCandidate, $length + 1)); //Example: 123As4$as4125sdasjfisghvcda$2512
if($translationRoute == $translatedRouteCandidateTrimmed) {
$url = route($namedRoute, $parameters, false);
$this->log('Found a localized named route in the language file resources/lang/'.App::getLanguage()->iso_2.'/site/routes. Routename: '.$namedRoute);
$this->log('That named route expands like this route("'.$namedRoute.'") to (including parameters): '.$url);
return $url;
}
}
return false;
}
}