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 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序