Reputation: 67
I have countries model, also tag and category. And URLs for them not contains prefix or ids
Mysite.org/spain <— this is country
Mysite/politics <- this is category
Mysite/population <— this is tag
How to check one one by one these URLs?
I try use middleware
Route::get(‘/{slug}’, ‘CountryController@show’)->middleware(‘CheckCountryPath’)
Route::get(‘/{slug}’, ‘CategoryController@show’)->middleware(‘CheckCategoryPath’)
Route::get(‘/{slug}’, ‘TagController@show’)->middleware(‘CheckTagPath’)
in middleware
public function handle($request, Closure $next)
{
// Contru checking logick
// if country model has this slug
return $next($request);
// else continue
}
How to say in middleware if model has not this slug continue checking other routs, do not redirect
Upvotes: 0
Views: 200
Reputation: 2951
You need to put your route at the end of your web.php
file.
You need to process your slug in a single Middlware that will redirect to the proper Controller:
class ChangeControllerMiddleware {
public function handle($request, Closure $next) {
$route = $request->route();
if($country) // Country Check Logic
$controller = '\App\Http\Controllers\CountryController@show';
// If not Country Category Logic ...
elseif($category)
$controller = '\App\Http\Controllers\CategoryController@show';
// else 404
else
abort(404);
$routeAction = array_merge($route->getAction(), [
'uses' => $controller,
'controller' => $controller,
]);
$route->setAction($routeAction);
$route->controller = false;
return $next($request);
}
}
Upvotes: 1