Laravel 5.2.35 - 无法设置Session :: flash

时间:2016-06-03 11:06:00

标签: php laravel laravel-5 laravel-5.2 flash-message

今天我尝试在Laravel 5.2.35项目中设置Session::flash('alert-info', 'This is a message')。但它不会显示。

我从web删除了routes.php中间件。但它仍然无效。

Routes.php

Route::group(['prefix' => 'client/{administration_id}', 'middleware' => ['loggedin']], function() {
    Route::get('/projects', ['as' => 'client.projects', 'uses' => 'ProjectController@listProjects']);
    Route::get('/project/new', ['as' => 'client.project.create', 'uses' => 'ProjectController@newProject']);
    Route::post('/project/new', ['as' => 'client.project.create.save', 'uses' => 'ProjectController@saveNewProject']);
});

ProjectController.php

<?php

namespace App\Http\Controllers;

use App\Projects;
use App\Http\Requests;
use Session;

class ProjectController extends Controller
{
    public function newProject($administration_id) {
        $customers = Customer::where('administration_id', $administration_id)->get();

        return view('client.projects.create', compact('customers'));
    }

    public function saveNewProject(Request $request, $administration_id) {

        $project = new Projects();
        $project->name = $request->name;
        $project->project_number = $request->code;
        $project->administration_id = $administration_id;
        $project->customer_id = $request->customer;

        if($project->save()) {
            Session::flash('alert-info', trans('projects.projects.create.success'));

            return redirect(route('client.projects', $administration_id));
        } else {
            Session::flash('alert-warning', trans('projects.projects.create.failure'));
            return redirect()->back();
        }
    }
}

Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'loggedin' => [
        'auth',
    ],

    'api' => [
        'throttle:60,1',
    ],
];

1 个答案:

答案 0 :(得分:0)

我从middleware web删除了RouteServiceProvider.php

旧代码:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace, 'middleware' => 'web',
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

新代码:

protected function mapWebRoutes(Router $router)
{
    $router->group([
        'namespace' => $this->namespace,
    ], function ($router) {
        require app_path('Http/routes.php');
    });
}

在此之后它工作正常,我不知道其他人是否有同样的问题,但也许这可能是一个解决方案。