MethodNotAllowedHttpException,重定向到404

pmi*_*nda 2 php routes laravel

这是一组控制器的列表:

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');
    });
Run Code Online (Sandbox Code Playgroud)

我在键入URL时要重定向到404错误:

http://myapp.com/some-prefix/ANYTHING-that-doesnt-match
Run Code Online (Sandbox Code Playgroud)

这是我收到下一个错误的时候:

(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)
Run Code Online (Sandbox Code Playgroud)

我在我的控制器中添加了一个failOrFind内部storeedit方法,因此我可以重定向404路由,如:

http://myapp.com/some-prefix/9999/edit
Run Code Online (Sandbox Code Playgroud)

价值9999不存在的地方,但我怎么能按照我的要求去做?

Leo*_*ent 12

App\Exception开拓handler.phprender()方法添加:

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

    return parent::render($request, $exception);
 }
Run Code Online (Sandbox Code Playgroud)

  • @leo请编辑你的代码`MethodNotAllowedHttpException`而不是`MethodNotAllowedHttpExceptionn`,最后注意两个"n". (3认同)