2017-04-25 7 views
0

を使用して認証されていない場合は認証されない場合は、(ログインページです)私のインデックスページへLaravel 5.4は、ユーザーが、私は、ユーザーをリダイレクトするミドルウェア

はそれを動作させるように見えることはできません特定のページにリダイレクトし、私は本当にルーティングと混同しています。

にHomeController

class HomeController extends Controller 
{ 

    /** 
    * Show the application dashboard. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     return redirect()->guest('/'); 
    } 
} 

ルーティング

// Index 
Route::get('/', [ 
    'as' => 'index', 
    'uses' => '[email protected]' 
]); 

UserControllerで

あなたは以下の通りですインデックス機能、でユーザコントローラへのリダイレクトを見るようにルーティング:

* __construct()はそう、それはミドルウェアを使用して「認証」を持っています。

public function __construct() 
{ 
    $this->middleware('auth'); 
} 

public function index(){ 

    // If user is logged 
    if(Auth::check()) { 

     // If user has NOT submitted information form redirect there, otherwise to categories 
     if(!Auth::user()->submitted_information) 
      return redirect()->route('information'); 
     else 
      return redirect()->route('categories'); 
    } 
    else 
     return view('index', ['body_class' => 'template-home']); 

} 

Handler.php

そして認証されていない機能認証のミドルウェア内部(例外/ Handler.php)

protected function unauthenticated($request, AuthenticationException $exception) 
    { 
     if ($request->expectsJson()) { 
      return response()->json(['error' => 'Unauthenticated.'], 401); 
     } 

     return redirect()->route('index'); 
    } 

私は今取得エラーがあります以下:

InvalidArgumentException in UrlGenerator.php line 304: 
Route [index] not defined. 

このエラーは、上記非認証関数で

return redirect()->route('index');ラインの起こります。

ここには何が欠けていますか?それ以上の情報が必要な場合はお気軽にお問い合わせください。

EDIT:今まで、私はUserControllerでから__construct()メソッドを削除し、それがどのような作品middleware使用するには、すべてのルートにweb.phpに挿入した場合。

Route::get('/categories', [ 
    'as' => 'categories', 
    'uses' => '[email protected]' 
])->middleware('auth'); 

しかし、私は自動的にそれを使用するために、使用するどのようなミドルウェアが指定しなくても、検索しようとしています。例えば

答えて

2

コード以下のようなあなたのルートを構築してみてください。

Route::group(['middleware' => ['auth']], function() { 
    // uses 'auth' middleware 
    Route::resource('blog','BlogController'); 
}); 

ルート::( '/マイページ'、 'にHomeControllerする@マイページ')を取得します。

はRedirectIfAuthenticatedという名前のミドルウェアクラスを開き、ハンドルfucntion にあなたはコードの下に書く:

if (!Auth::check()) { 
    return redirect('/mypage'); // redirect to your specific page which is public for all 
} 

はそれがあなたのために働くことを願っています。

1

あなたのルートは、ルーティングの詳細について

//インデックス

Route::get('/','[email protected]')->name('index); 

see hereようにする必要があります。

+0

いいえ、それは正しいルーティングを表示する助けにはなりませんでしたが、現在ネットワークループが常に "/" –

関連する問題