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?

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?