0

Laravel 4의 App::error 클래스를 사용하여 응용 프로그램 전체에서 Sentry 예외를 catch하고 withErrors() 함수를 사용하여 데이터를 다시 템플릿에 전달합니다.Laravel 4 'with data persistive

간단한 경로 :

routes.php

Route::post('/login... 

... 

$credentials = array(
    'email' => Input::get('email'), 
    'password' => Input::get('password') 
); 

$user = Sentry::authenticate($credentials); 

// Exception thrown... 
그런

는 예외를 잡을 : 뷰에서

exceptions.php

App::error(function(Cartalyst\Sentry\Users\WrongPasswordException $e) { 
    return Redirect::back()->withErrors(array('failed' => 'Email or password is incorrect'))->withInput(); 
}); 

:

을 당신이 시도에서 실패한 로그인 한 후 페이지를 새로 고칠 때

/views/login/login.blade.php

@if ($errors->has('failed')) 
    <strong>{{ $errors->first('failed') }}</strong> 
@endif 

문제는이러한 오류가 두 번 그들을 볼 수 있도록 유지됩니다. 두 번째 시간 새로 고침, 그들은 지웠다. 입력에 대해 동일하게 적용됩니다 (withInput() 전달).

오류가 경로 내에서 발견되면 (App:error 대신) 모든 것이 정상적으로 작동합니다. App::error 방법을 사용하여 저장된 데이터를 수동으로 지워야합니까?

답변

0

나는 항상 Session :: flash()를 사용하여 오류를 표시합니다. Flash는 한 요청에 대해 세션에 데이터를 설정하고 자동으로 설정을 해제합니다. 그래서 당신은

App::error(function(Cartalyst\Sentry\Users\WrongPasswordException $e) { 
    Session::flash('error', 'Email or password is incorrect.'); 
    return Redirect::back()->withInput(); 
}); 

같이 가서보기에이 잡을 수 :

@if($message = Session::get('success')) 
    <div class="alert-box success"> 
     {{ $message }} 
    </div> 
@endif 

@if($message = Session::get('error')) 
    <div class="alert-box alert"> 
     {{ $message }} 
    </div> 
@endif 

관련 메모에, 난과 같이 일반적인 시도 - 캐치 표기법을 따르도록 제안 :

try { 
    // do things that might throw an Exception here... 

} catch(Cartalyst\Sentry\Users\UserExistsException $e) { 
    // catch the Exception... 

    Session::flash('error', Lang::get('register.user_already_exists')); 
    return Redirect::action('[email protected]')->withInput(); 

} 

... 현재 App::error()으로 수행중인 작업이 그보다 조금 복잡 할 수 있습니다.