Devo gestire TokenMismatchException
in laravel 5 in modo tale che se il token non corrisponde visualizzerà un messaggio all'utente al posto dell'errore TokenMismatchException
.Handle TokenMismatchException in laravel 5
7
A
risposta
20
È possibile creare un numero personalizzato exception render nella classe App\Exceptions\Handler
(nel file /app/Exceptions/Handler.php
).
Ad esempio, per rendere una visione diversa, quando per l'errore TokenMismatchException
, è possibile modificare il metodo di render
a qualcosa di simile:
/**
* 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);
}
6
Avrete bisogno di scrivere una funzione di rendere l'errore TokenMismatchException. Aggiungerai questa funzione alla tua classe App \ Exceptions \ Handler (nel file /app/Exceptions/Handler.php) in questo modo:
// 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');
}
Problemi correlati
- 1. TokenMismatchException in VerifyCsrfToken - laravel 5.1
- 2. Laravel 5 TokenMismatchException su PHP 5.6.9
- 3. laravel TokenMismatchException
- 4. Laravel API TokenMismatchException
- 5. laravel 5.1 xmlHttpRequest AJAX POST TokenMismatchException
- 6. Come disabilitare i cookie in Laravel 5?
- 7. laravel 5 - NotFoundHttpException in linea RouteCollection.php 143
- 8. PHPWord in Laravel 5
- 9. laravel 5 ide autocompleto
- 10. Laravel 5 Error Reporting Suppression
- 11. PHP SoapClient in laravel 5
- 12. Installation Guzzle in Laravel 5
- 13. Switch in Laravel 5 - Blade
- 14. Eccezioni API in Laravel 5
- 15. laravel 5: MassAssignmentException in model.php
- 16. Creazione utente in Laravel 5
- 17. TRUNCATE ... CASCADE in Laravel 5
- 18. laravel 5 - impaginazione manuale
- 19. required_if Laravel 5 validation
- 20. Laravel 5, Visualizza :: Condividi
- 21. laravel 5 custom 404
- 22. Convalida serie laravel 5
- 23. laravel 5: Redirecting woes
- 24. AngularJS + Laravel 5 Authentication
- 25. Laravel 5 Classe admin non esiste
- 26. Laravel 5 - Pretty paginator
- 27. Laravel 5 + AngularJS html5Mode
- 28. Laravel 5 hasManyThrough
- 29. Laravel 5 localization: exclude/public/directory
- 30. Laravel 5 vista non trovata
Ciao Hiew. Ho appena provato il tuo metodo e sfortunatamente ottengo ancora un "TokenMismatchException in VerifyCsrfToken.php line 53" ogni volta che pubblico un falso csrf token/ – TechyTimo
Puoi aggiungere 'var_dump ($ e); die(); 'prima dell'istruzione' if' per verificare se il metodo 'rendered' è chiamato o meno. –
Sì, ora ho il dump della pagina dirty dell'oggetto (Illuminate \ Session \ TokenMismatchException) # 274 ... – TechyTimo