我需要在laravel 5中处理$scope.formData = {};
//...also $scope.formData = []; works too
,这样如果令牌不匹配,它会向用户显示一些消息而不是TokenMismatchException
错误。
答案 0 :(得分:22)
您可以在App\Exceptions\Handler
课程中创建自定义exception render(在/app/Exceptions/Handler.php
文件中)。
例如,要在TokenMismatchException
错误时呈现不同的视图,您可以将render
方法更改为以下内容:
/**
* Render an exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @param \Exception $e
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $e)
{
if ($e instanceof \Illuminate\Session\TokenMismatchException) {
return response()->view('errors.custom', [], 500);
}
return parent::render($request, $e);
}
答案 1 :(得分:7)
您需要编写一个函数来呈现TokenMismatchException错误。您将以这种方式将该函数添加到App \ Exceptions \ Handler类(在/app/Exceptions/Handler.php文件中):
// make sure you reference the full path of the class:
use Illuminate\Session\TokenMismatchException;
class Handler extends ExceptionHandler {
protected $dontReport = [
HttpException::class,
ModelNotFoundException::class,
// opt from logging this error to your log files (optional)
TokenMismatchException::class,
];
public function render($request, Exception $e)
{
// Handle the exception...
// redirect back with form input except the _token (forcing a new token to be generated)
if ($e instanceof TokenMismatchException){
return redirect()->back()->withInput($request->except('_token'))
->withFlashDanger('You page session expired. Please try again');
}