使用middleware。
在句柄方法中,您可以訪問$request
對象。當找不到路線重定向到您的後備路線。請參閱this question獲取當前網址的選項
編輯:可以在Laracasts forum中找到實現。海報想要保護管理路線:
public function handle($request, Closure $next)
{
$routeName = Route::currentRouteName();
// isAdminName would be a quick check. For example,
// you can check if the string starts with 'admin.'
if ($this->isAdminName($routeName))
{
// If so, he's already accessing an admin path,
// Just send him on his merry way.
return $next($request);
}
// Otherwise, get the admin route name based on the current route name.
$adminRouteName = 'admin.' . $routeName;
// If that route exists, redirect him there.
if (Route::has($adminRouteName))
{
return redirect()->route($adminRouteName);
}
// Otherwise, redirect him to the admin home page.
return redirect('/admin');
}
你想在Controller @ page方法中完全做什麼? – Abhishek