Flash会话在Laravel 5.2.39中不起作用

时间:2016-06-29 21:49:26

标签: php laravel homestead

我使用下面的代码重定向到控制器的路由,但它不起作用。视图无法从会话中获取值。

GameController

public function index()
{
    return view('game.index');
}

public function sync(){
    return redirect('/game')->with('status','[SUCCESS]');
}

查看

{{ session('status') }}
        @if(session('status'))
            asdasd
            @if(session('status') === '[SUCCESS]')
                <div class="row">
                    <div class="col-lg-12">
                        <div class="alert alert-success">Synced successfully!</div>
                    </div>
                </div>
            @elseif(session('status') === '[FAILED]')
                <div class="row">
                    <div class="col-lg-12">
                        <div class="alert alert-danger">Sync failed!</div>
                    </div>
                </div>
            @endif
        @endif

我已经运行php artisan route:list,结果如下所示

Screenshot

这是我的 route.php

Route::get('/', function () {
    return redirect('/home');
});
Route::get('/home', 'HomeController@index');
Route::get('/game', 'GameController@index');
Route::get('/game/sync', 'GameController@sync');

修改

试图运行此路线

Route::get('/test', function () {
    Session::put('status', 'Just a test');
    dd(Session::all());
});

结果

array:4 [▼
  "_token" => "9ZvL7FTzFt05rYXPviqhizohfFC6KWGqwMJVgYid"
  "_previous" => array:1 [▶]
  "flash" => array:2 [▶]
  "status" => "Just a test"
]

2 个答案:

答案 0 :(得分:0)

从您发布的代码中,我创建了一个具有相同版本的新Laravel项目,5.2.39。

这是完整的,有效的代码。

php artisan route:list

+--------+----------+-----------+------+-------------------------------------------+------------+
| Domain | Method   | URI       | Name | Action                                    | Middleware |
+--------+----------+-----------+------+-------------------------------------------+------------+
|        | GET|HEAD | /         |      | Closure                                   | web        |
|        | GET|HEAD | game      |      | App\Http\Controllers\GameController@index | web        |
|        | GET|HEAD | game/sync |      | App\Http\Controllers\GameController@sync  | web        |
|        | GET|HEAD | home      |      | App\Http\Controllers\HomeController@index | web        |
+--------+----------+-----------+------+-------------------------------------------+------------+

<强> GameController.php

// GameController.php
<?php
namespace App\Http\Controllers;

class GameController extends Controller {

    public function index()
    {
        return view('game.index');
    }

    public function sync() {
        return redirect('/game')->with('status','[SUCCESS]');
    }
}

<强>资源/视图/游戏/ index.blade.php

    {{ session('status') }}
    @if(session('status'))
        asdasd
        @if(session('status') === '[SUCCESS]')
            <div class="row">
                <div class="col-lg-12">
                    <div class="alert alert-success">Synced successfully!</div>
                </div>
            </div>
        @elseif(session('status') === '[FAILED]')
            <div class="row">
                <div class="col-lg-12">
                    <div class="alert alert-danger">Sync failed!</div>
                </div>
            </div>
        @endif
    @endif

然后,我在浏览器中转到/game/sync

<强>输出:

[SUCCESS] asdasd
Synced successfully!

答案 1 :(得分:0)

我的环境设置显然有问题。所以我从头开始重新安装所有东西,现在工作正常。