【发布时间】:2020-04-28 07:03:10
【问题描述】:
当我进入我的 /home 路由时,显示此问题重定向页面太多次。 我创建了一个名为 UserRole 的自定义中间件。在这个中间件中我设置了一个条件,如果 role==1 然后转到 root(/) 路由,否则转到 /home 路由。第一个条件正常工作,但其他条件抛出这个问题 重定向页面太多次。 我的代码如下:
web.php
Route::get('/', 'FrontendController@FrontPage');
Route::get('/home', 'HomeController@dashboard');
Kernel.php
'user_role' => \App\Http\Middleware\UserRole::class,
UserRole.php(midleware)
public function handle($request, Closure $next)
{
if(Auth::user()->role == 1){
return redirect('/');
}
else{
return redirect('/home');
}
}
Homecontroller.php
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('user_role');
}
public function dashboard()
{
return view('backend.dashboard');
}
}
FrontendController.php
class FrontendController extends Controller
{
public function FrontPage(){
$products=Product::all();
return view('frontend.main',compact('products'));
}
public function shopPage(){
$categories=Category::orderBy('name','asc')->get();
$title='All Product';
return view('frontend.shop',compact('categories','title'));
}
}
【问题讨论】:
-
为什么要使用中间件来处理这个问题?你在使用 laravel auth 脚手架吗?