Flash Session does not work, Laravel 5.1

Asked

Viewed 500 times

1

I’ve done this several times but for some reason now it’s not working, I can’t grab the flash from the view session

Userscontroller:

public function login_page(Request $request) {

    $title = "login";

    $dataToView = array(
        "title" => $title,
    );

    return view('layouts/user_login', $dataToView);
}

public function doLogin(Request $request) {

    $credentials = array(
        'email' => $request->input('email'),
        'password' => $request->input('password'),
    );

    if(!Auth::attempt($credentials)) {
        Session::flash('flash_error', 'Something went wrong with your login');
        return redirect(Session::get('lang'). '/user/login');
    }

    Session::flash('flash_success', 'You have logged in successfully ' .Auth::user()->name);

    return redirect(Session::get('lang'). '/user/login');
}

user_login.blade.php

div id="content">
<form method="POST" action="{{url($varNavBar['langLink']. '/user/login')}}">
    {!! csrf_field() !!}

    <div>
        Email
        <input type="email" name="email" value="{{ old('email') }}">
    </div>

    <div>
        Password
        <input type="password" name="password" id="password">
    </div>

    <div>
        <input type="checkbox" name="remember"> Remember Me
    </div>

    <div>
        <button type="submit">Login</button>
    </div>
</form>

@if(Session::has('flash_success'))
    <div class="alert alert-success">
        <b>{{Session::get('flash_success')}}</b>
    </div>
@elseif(Session::has('flash_error'))
    <div class="alert alert-danger">
        <b>{{Session::get('flash_error')}}</b>
    </div>
@endif

Routes:

Route::get('en/user/login', 'UsersController@login_page');
Route::post('en/user/login', 'UsersController@doLogin');

I can fix this with Session::set... and Session::pull.... But I’d like to know why I’m not getting the flash in the view

  • I think it might be your route interfering with this.

1 answer

1

I like to do so:

Routes.php

Route::controller('en/user/login', 'UsersController');

Userscontroller.php

# Alterei aqui para Index, que é a primeira página a sera acessada quando acessa a URL padrão.

public function getIndex(Request $request) {

    $title = "login";

    $dataToView = array(
        "title" => $title,
    );

    return view('layouts/user_login', $dataToView);
}

# Aqui coloquei o `post` e alterei o nome da página para a qual aparece no <form>.

public function postLogin(Request $request) {

    $credentials = array(
        'email' => $request->input('email'),
        'password' => $request->input('password'),
    );

    if(!Auth::attempt($credentials)) {
        Session::flash('flash_error', 'Something went wrong with your login');
        return redirect(Session::get('lang'). '/user/login');
    }

    Session::flash('flash_success', 'You have logged in successfully ' .Auth::user()->name);

    return redirect(Session::get('lang'). '/user/login');
}
  • This is not the solution, as I mentioned above, I can do it this way... With the set in the controller instead of flash. This solution is for not printing HTML tags from the controller, e.g. in the controller there is a $var = <b>hey</b> if sent to the view, the output of {{$var}} is "<b>hey</b>" when we really want to {!!$var!!} that is "hey"

  • It’s okay, I never had to touch that file to flash... Something tells me that somewhere in the middle I’m making another request and that flash is lost. But I don’t see where

Browser other questions tagged

You are not signed in. Login or sign up in order to post.