2016-08-20 4 views
0

나는 Halo 5 API를 사용 중이며 사용자는 게이머 태그로 등록 할 수 있습니다. 그들이 로그인하면 로그인 할 수 있으며 로그인하면 Halo 5에 대한 통계를 조회 할 수 있습니다. 그러나 분명히 일부 사용자는 합법적 인 게이머 태그를 입력하지 않을 것입니다. 그리고 그게 내 컨트롤러에서 프로필에 대한보기를 반환하는 몇 가지 유효성 검사가 필요했다.컨트롤러에 404 오류가 있는지 확인하십시오. Laravel 5

여기 컨트롤러의 간단한 예입니다 :

사용자가 존재하지 않는 경우 내 문제는, 그것은이 오류가 발생한다
public function index ($slug) { 

     // Emblem URL. 
     $getPlayerEmblemImage = app('App\Http\Controllers\Api\GetGeneralStatsController')->getPlayerEmblemImage($slug); 

     // Get General Player Arena Stats 
     $playerGeneralStats = app('App\Http\Controllers\Api\GetGeneralStatsController')->getPlayerArenaStats($slug); 
     $playerGeneralStatsArray = $this->getPlayerGeneralStatsArray($playerGeneralStats); 

     // Get the Spartan Rank and XP 
     $spartanRank = json_decode($playerGeneralStatsArray['SpartanRank'], true); 
     $XP = json_decode($playerGeneralStatsArray['Xp'], true); 
     $Gamer_Tag = json_encode($playerGeneralStatsArray['Gamer_Tag'], true); 

     $user = User::whereSlug($slug)->firstOrFail(); 

     return view('profile.index', 
      compact(
       'user', 
       'spartanRank', 
       'XP', 
       'getPlayerEmblemImage', 
       'Gamer_Tag', 
      ) 
     ); 
    } 

: 나는 몇 가지 검사를 할 수있는 방법

ClientException in RequestException.php line 107: Client error: GET https://www.haloapi.com/profile/h5/profiles/some%20useer/spartan resulted in a 404 Not Found response:

을, 그 플레이어가 발견되지 않으면 다른 결과를 반환합니까?

어쩌면 이렇게 될까요?

public function index ($slug) { 

    if (// Doesnt exist, show this) { 
    // do some other code here 
    return view('profile.index', compact('user')) 

    } else { 

    // Code here.... 

return view('profile.index', 
      compact(
       'user', 
       'spartanRank', 
      )) 
    } 

} 

답변

1

여기서 예외 처리를 사용할 수 있다고 생각합니다.

try{ 
    // Code here.... 

return view('profile.index', 
      compact(
       'user', 
       'spartanRank', 
      )) 
} 
} catch(ClientException $exception) { 
{ 
    // do some other code here 
    return view('profile.index', compact('user')) 
} 
컨트롤러에

가져 오기 use GuzzleHttp\Exception\ClientException;

사용 GuzzleHttp \ 예외 \의 ClientException;

+0

은 ** 나를 당신이 잡으려면 특정 예외와 예외를 교체 ** 말할 때 – David

+0

당신이 그 의견에 무엇을 의미합니까 것을 해보자? – David

+0

예외 대신에,'NotFoundException'을 사용하십시오. – jaysingkar

0

알 수 있습니다. 앱/예외의 변화 코드/Handler.php

public function render($request, Exception $e) 
    { 
     // Flash a success message saying you have successfully registered. 
     flash()->error('Error', 'That player has not played Halo, or does not exist.'); 

     return redirect()->back(); 

     // return parent::render($request, $e); 
    } 
관련 문제