File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/Http/Middleware/OldRouteResolver.php
<?php
namespace App\Http\Middleware;
use App\KommaApp\MagazineArticles\Models\MagazineArticleTranslation;
use App\KommaApp\NewsArticles\Models\NewsArticleTranslation;
use App\KommaApp\Sites\Models\Site;
use App\KommaApp\Sites\SiteServiceInterface;
use Illuminate\Http\Request;
class OldRouteResolver extends AbstractResolver
{
/** @var SiteServiceInterface */
private $siteService;
/** @var Site */
private $site;
public function __construct()
{
$this->siteService = \App::make(SiteServiceInterface::class);
parent::__construct();
}
/**
* 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);
$segments = $request->segments();
// The route we should redirect from the old website only have 1 segments, so if there are more return
if(sizeof($segments) != 1) return $next($request);
// Save the first segment (so old route)
$oldRoute = $segments[0];
// First try to find and possible redirect an old magazine article
if($magazineArticleTranslation = MagazineArticleTranslation::where('slug', $oldRoute)->first())
{
$redirectRoute = '/magazine/' . $magazineArticleTranslation->translatable->magazine()->translation->slug . '/' . $oldRoute;
return redirect($redirectRoute, 302);
};
// Then try to find and possible redirect an old news article
if($newsArticleTranslation = NewsArticleTranslation::where('slug', $oldRoute)->first())
{
$redirectRoute = '/nieuws/' . $oldRoute;
return redirect($redirectRoute, 302);
};
//Route resolver could not resolve route. Pass the request to the next middleware.
return $next($request);
}
}