mercredi 31 août 2016

Laravel 5.2 - Session flash data not set in service provider

I have created a frontend service provider for my app:

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Session;

class FrontendServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->share('message', !is_null(Session::get('message')) ? '<div class="alert alert-success"><strong>Success! </strong>'.Session::get('message').'</div>' : '');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

I am setting a flash message in the controller like so:

public function resend_welcome($token)
{
    $user_id = Crypt::decrypt($token);
    $user = User::findOrFail($user_id);
    if($user->meta->confirmed==1) {
        abort(404);
    }
    Event::fire(new UserRegistered($user));

    Session::flash('message', 'A new email has been sent.');

    return redirect(route('register.success', $token));
}

Then in my view I am trying to output $message like so:



The problem is $message is always NULL. If I use Session::get('message') in my view instead then it works and the flash message is displayed. Why is Session::get('message') always NULL in the service provider even though I have set it in the controller?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire