2016-04-02 4 views
1

管理パネル用のカスタムミドルウェアAdminAuthを作成しました。それは正常に動作しています。しかし、データベースに新しいエントリを作成すると、成功のためのフラッシュメッセージは表示されません。私のコードはルートファイルにあります:カスタムミドルウェアでセッションフラッシュメッセージが機能しない

});コントローラメソッドで

やコード:

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, 
]; 

答えて

0

return句の前にこれを追加すると、メッセージが表示されます。

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

まだ動作しません。 –

0

私は、マルチ認証のために、このtutorialを使用しています。

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() {