Laravel 5.2 Flash Session Not Working

2019-07-25 08:02发布

问题:

Here's my routes.php file:

Route::get('advertise', ['as' => 'advertise', 'uses' => 'AdvertiseController@index']);

Here's the relevant part of the App\Exceptions\Handler.php file:

public function render($request, Exception $e)
{
    switch ($e)
    {
        case ($e instanceof AdvertiserNotFoundException):
            return redirect()->route('advertise')->with('status', 'Advertiser not found.');
        default:
            return parent::render($request, $e);
    }
}

Here's how I'm (trying to) display the message:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

It is correctly catching the AdvertiserNotFoundException and redirecting to the route advertise but the response isn't carrying any flash data.

I'm not including the web middleware as all routes are covered by it by default.

EDIT 1

Here's the error log if it helps:

[2016-07-24 14:31:59] local.ERROR: exception 'App\Exceptions\AdvertiserNotFoundException' in C:\MyApp\app\Providers\RouteServiceProvider.php:31 Stack trace:
#0 [internal function]: App\Providers\RouteServiceProvider->App\Providers\{closure}('12d5763b-2a16-4...')
#1 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(1003): call_user_func(Object(Closure), '12d5763b-2a16-4...')
#2 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}('12d5763b-2a16-4...', Object(Illuminate\Routing\Route))
#3 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(885): call_user_func(Object(Closure), '12d5763b-2a16-4...', Object(Illuminate\Routing\Route))
#4 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(838): Illuminate\Routing\Router->performBinding('advertiser', '12d5763b-2a16-4...', Object(Illuminate\Routing\Route))
#5 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(825): Illuminate\Routing\Router->substituteBindings(Object(Illuminate\Routing\Route))
#6 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(691): Illuminate\Routing\Router->findRoute(Object(Illuminate\Http\Request))
#7 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Router.php(675): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#8 C:\MyApp\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(246): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#9 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#10 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(52): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#11 C:\MyApp\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php(44): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#12 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 C:\MyApp\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(136): call_user_func_array(Array, Array)
#14 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 C:\MyApp\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php(32): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#16 [internal function]: Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#17 C:\MyApp\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php(103): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#18 C:\MyApp\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(132): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#19 C:\MyApp\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php(99): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#20 C:\MyApp\public\index.php(54): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#21 {main}

回答1:

with() function is used pass data from controller to a view.

You need to set the data using session as follows:

case ($e instanceof AdvertiserNotFoundException):
    session('status', 'Advertiser not found.');
    return redirect()->route('advertise');