2016-12-06 5 views
0

ユーザーがログインすると、ホームページの代わりにプロフィールページにリダイレクトされます。別のコントローラにユーザープロファイルを取得するメソッドがあります。ユーザープロフィールにはユーザー名変数が必要なので、私は何をする必要があるのか​​分かりませんが、ユーザーがログインするときにはメールとパスワードの入力のみを求めています。laravelでの認証後のプロフィールページへのリダイレクト

ルートファイルですが、次の方法は認証コントローラとは異なるコントローラにあります。

Route::get('/user/{username}', [ 
    'uses' => '[email protected]', 
    'as' => 'profile.index', 
    'middleware' => ['auth'], 
    ]); 

私の以下の方法は私の認証コントローラにあります。

public function postSignin(Request $request) 
{ 
    $this->validate($request, [ 
     'email' => 'required', 
     'password' => 'required', 

     ]); 

    if (!Auth::attempt($request->only(['email', 'password']),  $request->has('remember'))) { 
     return redirect()->back()->with('info' , 'Could not sign you in with that info.'); 
    } 

     $user= User::where('username', $username)->first(); 
     return redirect()->route('profile.index') 
     ->with('info', 'You are now signed in.') 
     ->with('user', $user); 
    } 

次は正しくルートを構築するには...私のプロフィールコントローラに

public function getProfile($username) 
{ 
    $user= User::where('username', $username)->first(); 

    if (!$user){ 
     abort(404); 
    } 
    return view('profile.index') 
     ->with('user', $user); 
} 

答えて

0

である、あなたはここにユーザー名を渡す必要があります。

$user = User::where('username', $username)->first(); 
    return redirect()->route('profile.index', ['username' => $user->username]) 
    ->with('info', 'You are now signed in.') 
    ->with('user', $user); 
+0

私はそれを試みましたが、私はユーザー名が未定義になってしまいます。私が電子メールとパスワードを渡すときにどうすればいいのか分かりません – Slillz

+0

これはほんの一例ですが、あなたのデータベース構造は分かりませんが、実際には何かを渡す必要がありますそのルートに正しく作成する –

+0

はい私のデータベースの私のユーザ名フィールドは "username"ですが、私は... [Route:profile.index] [URI:user/{username}]の必須パラメータがありません。 – Slillz

0

は、電子メールからユーザー名を取得します提供するには$username変数をルーティングする:

public function postSignin(Request $request) 
{ 
     if (!Auth::attempt($request->only(['email', 'password']),$request->has('remember'))) 
     { 
      return redirect()->back()->with('info' , 'Could not sign you in with that info.'); 
     } 

    $username=User::where(['email'=>$request->email])->first()->username; 
    return redirect()->route('profile.index')->with('username', $username); 
} 
+0

私はそれを試みたときにエラーが発生しました:[Route:profile.index] [URI:user/{username}]に必要なパラメータがありません。 – Slillz

+0

@Slillz 'return redirect( 'user/{$ username}'); ' –

+0

彼らは私に教えてくれました:Application.php行のNotFoundHttpException 902: – Slillz

関連する問題