Sus*_*yal 1 php laravel laravel-4 cartalyst-sentry
我不希望在laravel 4登录后显示登录页面.如果登录用户想要访问登录页面,它应该重定向到主页('/').我正在使用Sentry进行身份验证.
filter.php
Route::filter(
'auth', function () {
if (!Sentry::check()) {
return Redirect::to('login');
}
}
Run Code Online (Sandbox Code Playgroud)
routes.php文件
Route::get('login', array('as' => 'login', function() {
return View::make('login');
}))->before('guest');
Route::post('login', 'AuthController@postLogin');
Run Code Online (Sandbox Code Playgroud)
AuthController.php
function postLogin() {
try {
// Set login credentials
$credentials = array(
'email' => Input::get('email'), 'password' => Input::get('password')
);
// Try to authenticate the user
$user = Sentry::authenticate($credentials, false);
if ($user) {
return Redirect::to('/');
}
} catch (Cartalyst\Sentry\Users\LoginRequiredException $e) {
return Redirect::to('login')->withErrors('Login field is required');
}
}
Run Code Online (Sandbox Code Playgroud)
成功登录后,如果请求登录页面,它仍会显示登录页面
如果您使用的是Laravel的默认guest过滤器,则它将无效,因为默认guest过滤器不会检查您的Sentry用户是否已登录.
试试这个:
Route::filter(
'filter', function () {
if (Sentry::check()) {
return Redirect::to('/');
}
}
Run Code Online (Sandbox Code Playgroud)
在routes.php中,过滤器已经应用于登录路由,因此事情应该以这种方式工作.