Laravel 5重定向循环错误 [英] Laravel 5 redirect loop error
问题描述
我试图制作一个登录和管理脚本,问题是我有一个重定向循环,我不知道为什么.
I trying to make a login and admin script, the problem is that I have a redirect loop I dont know why.
我想要登录用户,并且可以位于/
路径中,而不位于/home
中.
I want the login users and can be in the /
path not /home
.
如果将return new RedirectResponse(url('/'));
更改为return new RedirectResponse(url('/anotherpage'));
,它可以工作,但我想成为/
If change return new RedirectResponse(url('/'));
to return new RedirectResponse(url('/anotherpage'));
it works but I want to be /
路线:
Route::get('/', [
'as' => 'home', 'uses' => 'HomeController@index'
]);
// Tutorials Routes
Route::get('/tutorials', 'HomeController@tutorials');
Route::get('/tutorials/{category?}', 'HomeController@tutorialsCategory');
Route::get('/tutorials/{category?}/{lesson?}', 'HomeController@tutorialsLesson');
// Courses and Series Routes
Route::get('/courses-and-series', 'HomeController@coursesandseries');
// Admin Routes
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function()
{
Route::get('/admin', function()
{
return 'Is admin';
});
});
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
管理员中间件:
public function handle($request, Closure $next)
{
if (Auth::user()->type != 'Admin')
{
return abort(404);
}
return $next($request);
}
RedirectIfAuthenticated:
public function handle($request, Closure $next)
{
if ($this->auth->check())
{
return new RedirectResponse(url('/'));
}
return $next($request);
}
家庭控制器:
class HomeController extends Controller {
public function __construct()
{
$this->middleware('guest');
}
public function index()
{
return view('home');
}
public function tutorials()
{
return view('pages.tutorials');
}
public function tutorialsCategory()
{
return view('pages.tutorials');
}
public function tutorialsLesson()
{
return view('pages.single');
}
public function coursesandseries()
{
return view('pages.coursesandseries');
}
public function single()
{
return view('pages.single');
}
}
推荐答案
您正在经历这些重定向循环,因为HomeController中的所有方法都受到来宾中间件的保护.
You are having these redirection loops because all the methods in HomeController are protected by Guest Middleware.
由于您希望将经过身份验证的用户重定向到HomeController @ index
Since you wish to redirect authenticated users to HomeController@index
从HomeController中删除$this->middleware('guest');
Remove $this->middleware('guest');
from HomeController
或
修改来宾中间件以忽略索引方法
Modify the Guest Middleware to ignore index method
$this->middleware('guest', ['only' => ['tutorials','tutorialsCategory']])
列出除索引方法之外的其他您希望用来宾中间件保护的方法
List other methods you wish to protect with Guest Middleware excluding Index method
这篇关于Laravel 5重定向循环错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!