MethodNotAllowedHttpException,重定向到404 [英] MethodNotAllowedHttpException, redirect to 404

查看:72
本文介绍了MethodNotAllowedHttpException,重定向到404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一组控制器的列表:

This is the list of a group of controllers:

Route::group([
        'prefix'     => 'some-prefix',
    ], function () {
        Route::get('/', 'MyController@index')->name('some-prefix');
        Route::post('/get', 'MyController@getData')->name('some-prefix.get');
        Route::get('/getall/{type}', 'MyController@getAllData')->name('some-prefix.getall');
        Route::get('/create', 'MyController@create')->name('some-prefix.create');
        Route::post('/', 'MyController@store')->name('some-prefix.store');
        Route::get('/edit', 'MyController@edit')->name('some-prefix.edit');
        Route::get('/{id}/edit/', 'MyController@edit')->name('some-prefix.edit');
        Route::put('/{id}', 'MyController@update')->name('some-prefix.update');
        Route::get('/cambiarestado/{id}', 'MyController@cambiarestado')->name('some-prefix.cambiarestado');
    });

我想在输入URL时重定向到404错误:

I want to redirect to a 404 error when I type the URL:

http://myapp.com/some-prefix/ANYTHING-that-doesnt-match

这是我遇到下一个错误的时间:

Here's when I got the next error:

(1/1) MethodNotAllowedHttpException
in RouteCollection.php (line 251)
at RouteCollection->methodNotAllowed(array('PUT'))
in RouteCollection.php (line 238)
at RouteCollection->getRouteForMethods(object(Request), array('PUT'))
in RouteCollection.php (line 176)

我在控制器的 store edit 方法中放入了 failOrFind ,因此我可以选择404条路由,例如:

I put a failOrFind inside my store and edit methods in my controller, so I can redict to 404 routes like:

http://myapp.com/some-prefix/9999/edit

不存在值 9999 的地方,但是我该怎么做?

where the value 9999doesn't exist, but how can I do what I asked?

推荐答案

转到 App \ Exception render()中打开 handler.php code>方法添加:

Go to App\Exception open up handler.php in render() method add:

 public function render($request, Exception $exception)
 {
    if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
      return abort('404');
    }

    return parent::render($request, $exception);
 }

这篇关于MethodNotAllowedHttpException,重定向到404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆