简体   繁体   English

Laravel 5.2 Flash会话无法正常工作

[英]Laravel 5.2 Flash Session Not Working

Here's my routes.php file: 这是我的routes.php文件:

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

Here's the relevant part of the App\\Exceptions\\Handler.php file: 这是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);
    }
}

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. 它可以正确捕获AdvertiserNotFoundException并重定向到advertise路由,但响应未携带任何闪存数据。

I'm not including the web middleware as all routes are covered by it by default. 我不包括Web中间件,因为默认情况下所有路由都包含在其中。

EDIT 1 编辑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}

with() function is used pass data from controller to a view. with()函数用于将数据从控制器传递到视图。

You need to set the data using session as follows: 您需要使用会话来设置数据,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM