我已经在laravel helper类中创建了函数来检查Authentication app/lib/Auth.php
class Auto extends \BaseController {
public static function logged() {
if(Auth::check()) {
return true;
} else {
$message = array('type'=>'error','message'=>'You must be logged in to view this page!');
return Redirect::to('login')->with('notification',$message);
}
}
}
在我的控制器中
class DashboardController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
Auto::logged();
return View::make('dashboard.index');
}
如果未登录,我希望它重定向到登录路由,但是它会加载dashboard.index
带有消息“您必须登录才能查看此页面!”的视图。
如何使用此消息重定向到登录路由?
为什么要为此创建新的助手功能。Laravel已经为您处理了。请参阅app/filters.php
。您将看到类似以下的身份验证过滤器
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('/')->with('message', 'Your error message here');
}
}
});
您可以确定用户是否已通过身份验证,如下所示
if (Auth::check())
{
// The user is logged in...
}
阅读有关Laravel文档上的身份验证的更多信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句