Flash Session does not work, Laravel 5.1 [closed]

1

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

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 solve this with Session::set... and Session::pull... . But I wondered why I was not catching the flash in the view

    
asked by anonymous 10.12.2015 / 12:04

1 answer

1

I like to do this:

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');
}
    
10.12.2015 / 12:11