2017-05-19 8 views
0

新しいユーザーを登録したい:クライアントIDとクライアントシークレットを使用してAPIを呼び出し、すべてが正常であればユーザーを保存する。そうでない場合、私はエラーメッセージでリダイレクトします。laravel apiへの呼び出しによって有効なアクセストークンが返された場合

バリデーター内の登録ルートにリダイレクトしようとすると、このエラーが発生します。という文字列のメンバー関数validate()を呼び出します。

protected function validator(array $data) 
{ 
    $messages = [ 
     'client_secret.size' => 'Secret Id must be exactly 36 characters', 
    ]; 

    $client_id = $data['client_id']; 
    $client_secret = $data['client_secret']; 

    $access = $this->getAccessToken($client_id, $client_secret); 
    if($access == false){ 
     return route('register'); 
} 

return Validator::make($data, [ 
     'name' => 'required|string|max:255', 
     'email' => 'required|string|email|max:255|unique:users', 
     'password' => 'required|string|min:6|confirmed', 
     'role' => 'required|string', 
     'country' => 'required|string', 
     'client_id' => 'required|string', 
     'client_secret' => 'required|string|size:36' 
    ], $messages); 
} 
+0

これは誤った実装です。バリデータ関数は、文字列が返されたアクセストークンの取得に失敗した場合に 'route( 'register')'を返します。しかし、アクセストークンがフェッチされると同時に、バリデータインスタンスを返します。これを呼び出すコードは、最初のシナリオで失敗する 'validate'メソッドを実行しようとします。そしてこの関数がコード化されている方法では、そこからリダイレクトすることはできません。 – Sandeesh

答えて

1

私はあなたの答えを見る前にそれをしました。私はそれが似ていると思う。

protected function validator(array $data) 
{ 
    $messages = [ 
     'client_secret.size' => 'Secret Id must be exactly 36 characters', 
     'access_token.required' => 'We could not get an access token, make sure that the client id and the client secret are correct' 
    ]; 

    $input = [ 
     'name' => 'required|string|max:255', 
     'email' => 'required|string|email|max:255|unique:users', 
     'password' => 'required|string|min:6|confirmed', 
     'role' => 'required|string', 
     'country' => 'required|string', 
     'client_id' => 'required|string', 
     'client_secret' => 'required|string|size:36', 
     'access_token' => 'required|string|min:10' 
    ]; 
    $client_id = $data['client_id']; 
    $client_secret = $data['client_secret']; 


    $access = $this->getAccessToken($client_id, $client_secret); 

    if($access == false){ 
     $data['access_token'] = 'false'; 
    }else{ 
     $data['access_token'] = $access ; 
    } 

    return Validator::make($data, $input, $messages); 
} 
0

これは誤った実装です。バリデータ関数は、この文字列が返されたアクセストークンの取得に失敗した場合にroute( 'register')を返します。しかし、アクセストークンがフェッチされると同時に、バリデータインスタンスを返します。これを呼び出すコードは、最初のシナリオで失敗するvalidateメソッドを実行しようとします。そしてこの関数がコード化されている方法では、そこからリダイレクトすることはできません。あなたは本当に、あなたがロジック

try { 
    $validator = $this->validator($data); 
} catch (\Exception $e) { 
    return redirect()->route('register'); 
} 

if ($validator->fails()) { 
    // handle 
} 

OR

検証方法

if($access == false) { 
    return null; 
} 

コールインを呼び出すと、この

検証方法

if($access == false) { 
    throw new \Exception('Failed to get access token'); 
} 

ような何かを行うことができますする必要がある場合g論理

$validator = $this->validator($data); 

if ($validator === null) { 
    return redirect()->route('register'); 
} 

if ($validator->fails()) { 
    // handle 
} 
関連する問題