2017-05-19 2 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

잘못된 구현입니다. validator 함수는 반환 된 액세스 토큰을 얻지 못하면'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

잘못된 구현입니다. validator 함수는 반환 된 액세스 토큰을 얻지 못하면 route ('register')를 반환합니다. 그러나 동시에 액세스 토큰을 가져 오는 경우 유효성 검사기 인스턴스를 반환합니다. 이것을 호출하는 코드는 첫 번째 시나리오에서 실패 할 validate 메소드를 실행하려고 시도합니다. 그리고이 함수가 코드화 된 방식으로, 그 안에서 리다이렉트 할 수 없습니다. 당신은 정말 당신이

검사기 방법

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

호출 로직

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

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

또는

검사기 방법

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

전화도 같은 일을 할 수해야하는 경우 g 논리

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

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

if ($validator->fails()) { 
    // handle 
} 
관련 문제