在拉拉维尔助手功能中重定向


redirect in laravel helper function

>我在 laravel 助手类中创建了函数来检查 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视图并显示消息"您必须登录才能查看此页面!

如何使用此消息重定向到登录路由?

为什么要为此创建新的帮助程序函数。拉拉维尔已经为您处理好了。请参阅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文档上的身份验证.

这应该是工作:

 /**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    if(Auto::logged()) {
       return View::make('dashboard.index');
    }
}