简体   繁体   中英

MethodNotAllowedHttpException redirect to home page

laravel 5中发生MethodNotAllowedHttpException时是否可以将用户重定向到主页,或者我应该修改哪个模板以编辑MethodNotAllowedHttpException的显示?

Open your app/Exception/Handler.php

Here you will find a render method.

In render method add this line before your return statement :

if($e instanceof MethodNotAllowedHttpException) 
return redirect('/');

Don't forget to import the exception first:

use Symfony\\Component\\HttpKernel\\Exception\\MethodNotAllowedHttpException;

rfc2616 10.4.6 405 Method Not Allowed

in Laravel if you specify your .env

APP_ENV=production
APP_DEBUG=false

and create

resources/views/errors/405.blade.php

it will render that file each time an 405 code occurs, i don't think the redirect is possible , but you can always add some javascript into 405 error template for the user to be redirected.

setTimeout(function(){ window.location.href='/'; },10000);

you will need to import the correct name space first before you can successfully catch MethodNotAllowedException.

use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;

source: Laravel 5 - How do I handle MethodNotAllowedHttpException

you can use ExceptionHandler

open app/Exception/Handler.php change render method

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException)
    {
        return \Redirect::to('/');
    }
    return parent::render($request, $e);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM