duan5731
2015-01-12 09:15
浏览 109
已采纳

在laravel辅助函数中重定向

I have created function in laravel helper class to check Authentication in 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);    
        }
    }
}

In my controller

class DashboardController extends \BaseController {

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

I expect it redirect to login route if not logged, but it load dashboard.index view with message 'You must be logged in to view this page!'.

How can I redirect to login route with this message?

图片转代码服务由CSDN问答提供 功能建议

我在laravel helper类中创建了一个函数来检查 app / lib / Auth.php中的身份验证

 类自动扩展\ BaseController {
 
公共静态函数记录(){
 if(Auth:  :check()){
 return true; 
} else {
 $ message = array('type'=>'error','message'=>'您必须登录才能查看此页面!  '); 
 
返回Redirect :: to('login') - > with('notification',$ message);  
} 
} 
} 
   
 
 

在我的控制器中

 类DashboardController extends \ BaseController {\  n 
 / ** 
 *显示资源列表。
 * 
 * @return响应
 * / 
公共函数索引()
 {
 
Not :: logged(); 
 返回View :: make('dashboard.index'); 
} 
   
 
 

如果没有记录,我希望它重定向到登录路由,但它加载 dashboard.index 视图,其中包含“您必须登录才能查看此页面!”。

如何使用此消息重定向到登录路线? \ n

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • drugs3550 2015-01-12 09:25
    最佳回答

    Why you want to create new helper function for that. Laravel already handle it for you. See app/filters.php. You will be see authentication filter like the following

    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');
            }
        }
    });
    

    You can determine if user is Authenticated or not like the following

    if (Auth::check())
    {
        // The user is logged in...
    }
    

    Read more about Authentication on Laravel doc.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题