我使用Laravel4的App::error类在整个应用程序中捕获Sentry异常,并使用withErrors()函数将数据传递回模板。
简单路由:
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方法手动清除存储的数据吗?
发布于 2013-05-21 05:32:21
我总是使用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-catch符号,如下所示:
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('RegisterController@getIndex')->withInput();
}..。因为您目前使用App::error()所做的事情可能比这更麻烦一些。
https://stackoverflow.com/questions/16500791
复制相似问题