Laravel 5.2闪存会话不工作(Laravel 5.2 Flash Session Not W

2019-09-27 09:36发布

这是我的routes.php文件:

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

下面是相关的部分App\Exceptions\Handler.php文件:

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);
    }
}

下面是我如何(试图)显示以下信息:

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

这是正确捕捉AdvertiserNotFoundException和重定向到路由advertise ,但响应没有携带任何闪存数据。

我不包括网络中间件作为所有路线都被它覆盖默认。

编辑1

这里的错误日志,如果有帮助:

[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}

Answer 1:

with()函数是用来通数据从控制器的图。

您需要设置使用会话如下数据:

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


文章来源: Laravel 5.2 Flash Session Not Working