laravel catch tokenmismatchexception

Solutions on MaxInterview for laravel catch tokenmismatchexception by the best coders in the world

showing results for - "laravel catch tokenmismatchexception"
Roberto
12 Feb 2016
1<?php namespace App\Exceptions;
2use Exception;
3use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
4use Illuminate\Session\TokenMismatchException;
5
6
7class Handler extends ExceptionHandler {
8
9
10    /**
11     * A list of the exception types that should not be reported.
12     *
13     * @var array
14     */
15    protected $dontReport = [
16        'Symfony\Component\HttpKernel\Exception\HttpException'
17    ];
18    /**
19     * Report or log an exception.
20     *
21     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
22     *
23     * @param  \Exception  $e
24     * @return void
25     */
26    public function report(Exception $e)
27    {
28        return parent::report($e);
29    }
30    /**
31     * Render an exception into an HTTP response.
32     *
33     * @param  \Illuminate\Http\Request  $request
34     * @param  \Exception  $e
35     * @return \Illuminate\Http\Response
36     */
37    public function render($request, Exception $e)
38    {
39        if ($e instanceof TokenMismatchException){
40            // Redirect to a form. Here is an example of how I handle mine
41            return redirect($request->fullUrl())->with('csrf_error',"Oops! Seems you couldn't submit form for a long time. Please try again.");
42        }
43
44        return parent::render($request, $e);
45    }
46}
47