会话Flash消息在自定义中间件

时间:2016-04-02 06:52:26

标签: laravel

我为管理员面板制作了一个名为AdminAuth的自定义中间件。它工作正常。但是当我在数据库中创建一个新条目时,我没有获得任何成功的flash消息。这是我的路线文件中的代码:

Route::group(['middleware' => ['web']], function () {

Route::group(['middleware' => ['admins']], function () {

    Route::get('/admin', function(){
        return view('admin.welcome');
    });

    Route::get('/category', ['as' => 'category.index', 'uses' => 'Admin\CategoryController@index']);
    Route::post('/category/create', ['uses' => 'Admin\CategoryController@postCreate']);
    Route::get('/category/edit/{id}', ['as' => 'category.edit', 'uses' => 'Admin\CategoryController@getEdit']); 


});

});

和控制器方法中的代码:

public function postCreate(Request $request)
{
    $rules = [
        'name' => 'required|min:2,max:40|unique:categories'    
    ];

    if ($request->input('parent_id')) {
        $rules = array_add($rules, 'category_pic', 'required');
    }


    $this->validate($request, $rules);

    if ($request->hasFile('category_pic')) {
        $file = $request->file('category_pic');
        $file_name = $file->getClientOriginalName();
        $file_uploaded = $file->move('upload/category', $file_name);
        if (!$file_uploaded) {
            return redirect()
                ->route('category.index')
                ->with('warning', 'Category picture can not be uploaded.');                 
        }
    } 

    $category = new Category();            
    $category->name = $request->input('name');
    $category->slug = str_slug($request->input('name'));
    $category->parent_id = $request->input('parent_id'); 
    $category->description = $request->input('description');
    $category->admin_id= auth()->guard('admins')->user()->id;
    if ($request->hasFile('category_pic')) {
        $category->category_pic = $request->file('category_pic')
                                    ->getClientOriginalName();
    }
    if ($category->save()) {
        return redirect()
            ->route('category.index')
            ->with('success', 'Category created successfully.');                
    } else {
        return redirect()
            ->route('category.index')
            ->with('success', 'Category Not created.');              
    }   

}

和视图模板中的代码:

@if(Session::has('success'))
<div class="flash-message flash-message-success">
    {{ Session::get('success') }}
</div>
@endif

我找不到自己的错误。我该如何解决这个问题?

我已经在内核中注册了我的中间件:

    protected $routeMiddleware = [
    'auth' => \theGrocer\Http\Middleware\Authenticate::class,
    'admins' => \theGrocer\Http\Middleware\AdminAuth::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \theGrocer\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];

2 个答案:

答案 0 :(得分:1)

我使用此tutorial进行多重身份验证。

我终于找到了flash消息的解决方案:

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
//        \Illuminate\Session\Middleware\StartSession::class,
//        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    ];

从中间件中删除StartSessionShareErrosFromSession并更改Route group middleware -> middlewareGroups。这对我有用!

Route::group(['middlewareGroups' => ['web', 'admin']], function () {

答案 1 :(得分:0)

return子句之前添加此内容,您将看到一条消息:

Session::flash('success', 'Post was created successfully');