kbhatta
kbhatta

Reputation: 457

redirect in laravel helper function

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?

Upvotes: 3

Views: 2423

Answers (2)

Set Kyar Wa Lar
Set Kyar Wa Lar

Reputation: 4634

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.

Upvotes: 1

goldlife
goldlife

Reputation: 1989

this should be work :

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

Upvotes: 0

Related Questions