File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Http/Middleware/SiteResolver.php
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Str;
/**
* Class SiteResolver
*
* Does set the current site depending on the host / domain name using the site config file
*/
class SiteResolver extends AbstractResolver
{
/**
* Set site on Application.
*
* @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)) {
// Enforce kms to .be
if(app()->environment('production') && Str::startsWith($request->path(), 'kms') && $request->root() !== 'https://farmfun.be') {
return redirect('https://farmfun.be/'.$request->path());
}
return $next($request);
}
$site = $this->siteService->getCurrentSite();
if (! $site->exists && ! config('app.multipleSites')) {
throw new \RuntimeException("SiteResolver: This site doesn't exists. Make a new Site with this slug:'".$siteSlug."' or rename the desired site to this slug.");
}
// Determine the site version (so for now either 'nl' or 'be'
if(Str::contains($request->root(), [':8008', 'farmfun.nl'])) {
config()->set('site.site_version', 'nl');
config()->set('mail.from.address', 'info@farmfun.nl');
\App::setLocale('nl_nl');
}
// Only check for redirects on production
if (\App::environment() == 'production') {
$redirectPath = $this->getHttpsRedirectPathIfNeeded($request->root(), $request->path());
if (! empty($redirectPath)) {
return redirect($redirectPath);
}
}
// Check if defined language is one of the languages of this site
// if not throw error, because it shouldn't happen...
if (! $site->languages->contains(\App::getLanguage())) {
throw new \InvalidArgumentException('SiteResolver: Defined app language with iso_2 ('.\App::getLanguage()->iso_2.") doesn't exist in this site (id: ".$site->id.'), so check how this could happen.');
}
//Return the Request, and check the other routes
return $next($request);
}
/**
* Get Https redirect path if needed
*
* @param string $siteSlug
* @param string $root
* @param string $path
* @return string|null
*/
private function getHttpsRedirectPathIfNeeded(string $root, string $path)
{
// If we aren't on production we don't want to redirect
if (Str::contains($root, 'farmfun.nl')) {
if ($root !== 'https://farmfun.nl') {
$redirectPath = 'https://farmfun.nl';
if (isset($path) && ! in_array($path, ['', '/', 'index.php'])) {
$redirectPath .= '/'.$path;
}
return $redirectPath;
}
}
else {
if ($root !== 'https://farmfun.be') {
$redirectPath = 'https://farmfun.be';
if (isset($path) && ! in_array($path, ['', '/', 'index.php'])) {
$redirectPath .= '/'.$path;
}
return $redirectPath;
}
}
return null;
}
}