Laravel 5.2 Session flash not working even with web middleware

后端 未结 6 1451
孤城傲影
孤城傲影 2020-12-29 15:25

I am trying to implement flash messaging using sessions but am unable to do so.

In my controller I have:

public function store(Request $request) {
          


        
相关标签:
6条回答
  • 2020-12-29 15:57

    Build your Session flash info by using this code:

    <?php
    
    Session::flash("Donald", "Duck")
    // Or in your code style.
    $request->session()->flash("Donald", "Duck")
    ?>
    

    Check it in your view with:

    @if(Session::has("Donald")
        {{Session::get("Donald")}}
    @endif
    

    You forget to use $request :)

    0 讨论(0)
  • 2020-12-29 16:00

    I dont know why but on Windows you need changes in your routes: middleware to middlewareGroups, like that:

    change middleware to middlewareGroups

    So, in your app\Kernel.php, you need put the StartSession at first on array of middleware group web:

    put the StartSession at first on array of middleware group web

    0 讨论(0)
  • 2020-12-29 16:04

    This is more than likely because of a change that was made to the Laravel framework (v5.2.27) that all routes by default are part of the "web" middleware, so assigning it again in your routes.php file ends up assigning it twice.

    The solution is either to remove the "web" middleware from your routes OR remove the automatic assignment from the RouteServiceProvider.

    Before the Laravel update:

    // RouteServiceProvider.php
    $router->group(['namespace' => $this->namespace], function ($router) {
        require app_path('Http/routes.php');
    });
    

    After the Laravel update:

    // RouteServiceProvider.php
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
    

    Notice how the new update automatically applies the "web" middleware to all routes. Simply remove it here if you wish to continue using Laravel 5.2 as you have before (manually assigning "web" middleware in your routes.php).

    0 讨论(0)
  • 2020-12-29 16:08

    In Controller:

    use Session,Redirect;
    
    public function store(Request $request) 
    {
    Session::flash('donald', 'duck');
    Session::put('mickey', 'mouse');
    return Redirect::to('/customers')->with('bugs', 'bunny');
    }
    

    In 'view' check the data is getting or not:

    <?php
    print_r($bugs);die;
    ?>
    

    Good Luck :)

    0 讨论(0)
  • 2020-12-29 16:10

    I use the following:

    In my controller:

    public function xyz(){
       // code
    
       // This
       return redirect()->action('homeController@index')->with('success', 'Check! Everything done!');
    
        // Or this
        return redirect('/index')->with('success', 'Check! Everything done!');
    }
    

    In my view:

    @if(session('success'))
        {{ session('success') }}
    @endif
    

    Nothing else. The web-middleware is assigned to every route.

    0 讨论(0)
  • 2020-12-29 16:15

    Fixed the issue by replacing

    Route::group(['middleware' => ['web']], function () {
       ...
    });
    

    with

    Route::group(['middlewareGroups' => ['web']], function () {
       ...
    });
    

    No idea why this works though when all the documentation suggests that we use ['middleware' => ['web']]

    0 讨论(0)
提交回复
热议问题