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 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制