Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jpeterson579's avatar

middleware web doesnt allow sending session flash?

Laravel 5.2 What is the purpose of route:group with a middleware of 'web' Why cant I send a session->flash when the route is in this group?

route.php

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

}
0 likes
5 replies
jpeterson579's avatar

So for example I have

Route::group(['middleware' => 'web'], function () {
    Route::auth();    
    Route::post('favorites', ['as' => 'favorites.store', 'uses' => 'PostController@addToFavorites']);
}

PostController

public function addToFavorites(Request $request, $post_id)
 {
   // SweetAlert Flash message - type, title, message
   flash('success', 'Success!', 'You are no longer following ' . $postName);
   return redirect()->back();
  }

App/http/Flash.php

<?php
namespace App\Http;
class Flash {
public function message($type, $title, $message)
{
    session()->flash('flash_message', [
        'type'      => $type,
        'title'     => $title, 
        'message'   => $message
    ]);
}
}

View.blade.php

@if (session()->has('flash_message'))
<script type="text/javascript">
    swal({
      title: "{{ session('flash_message.title') }}",
      text: "{{ session('flash_message.message') }}",
      type: "{{ session('flash_message.type') }}",
      timer: 8000,
      showConfirmButton: "flase"
    });
</script>
@endif

When i take the route out of the middleware web, the flash message works, however when it is inside of the web group, it doesn't work...

tykus's avatar

You no longer need to be explicit about web middleware, it is there by default

However, keep in mind the web middleware group is already applied to your routes by default since the RouteServiceProvider includes it in the default middleware group.

source - https://laravel.com/docs/5.3/releases#laravel-5.3

jpeterson579's avatar

Ok so going back to the original question

What was the purpose of having route:group with a middleware of 'web'?

And why am I not able to session->flash when the route is in this group?

Should I just get rid of the group all together in my routes.php file?

Please or to participate in this conversation.