4

In my controller I have a function to login a user.

In case the login was successful I can simply use return Redirect::back().

My problem starts when the credentials are incorrect and I want to redirect with a flash message.

I know I can chain the with method to the Redirect, but that would send the data to the specific view, and NOT to the layout, where the login HTML lies.

I could load a view like so:

$this->layout
     ->with('flash',$message)
     ->content = View::make('index');

But I need to redirect back to the referring page.

Is it possible to redirect while passing data to the layout?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Matanya
  • 6,233
  • 9
  • 47
  • 80

1 Answers1

7

The Laravel Validator class handles this quite well.... The way I usually do it is to add a conditional within my layout/view in blade...

{{ $errors->has('email') ? 'Invalid Email Address' : 'Condition is false. Can be left blank' }}

This will display a message if anything returns with an error.. Then in your validation process you have...

 $rules = array(check credentials and login here...);

$validation = Validator::make(Input::all(), $rules);

if ($validation->fails())
{
    return Redirect::to('login')->with_errors($validation);
}

This way...when you go to the login page, it will check for errors regardless of submission, and if it finds any, it displays your messages.

EDITED SECTION For dealing with the Auth class.. This goes in your view...

@if (Session::has('login_errors'))
    <span class="error">Username or password incorrect.</span>
@endif

Then in your auth...something along these lines..

 $userdata = array(
    'username'      => Input::get('username'),
    'password'      => Input::get('password')
);
if ( Auth::attempt($userdata) )
{
    // we are now logged in, go to home
    return Redirect::to('home');
}
else
{
    // auth failure! lets go back to the login
    return Redirect::to('login')
        ->with('login_errors', true);

}
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • Thanks. While possible, logging a user is usually done with the Auth class – Matanya Apr 04 '13 at 16:04
  • 2
    Yeah, you would use Session::has('login_errors') in this case – Kylie Apr 04 '13 at 16:09
  • Hi @KyleK, can you explain more about with('login_errors', true), why need to be login_erros, and what is the 2nd parameter doing? Thanks! – Shiro Jun 19 '13 at 09:46
  • 1
    @Shiro, 'login_errors' is just an arbitrary name.... It really could be anything you want. And the 'true' parameter is just setting the value of 'login_errors' to true....cuz when login fails, we want this value to be true....so that in the view, that if statement in the view then checks if 'login_errors' is true, and displays a message. Basically..the ->with() method is just a key/value data pair....so first parameter is the key, second is the value. (in this case just a boolean)......most of the time you usually use ->with() to pass data to the view. – Kylie Jun 19 '13 at 15:58
  • haha.. thanks for the explaination. Now I got the big picture. First I thought login_errors is some kind of preset function key word or what. I can't get much documentation about this yet. Thank you so much Kylek! – Shiro Jun 20 '13 at 01:15