MethodNotAllowedHttpException, redirect to 404

徘徊边缘 提交于 2021-02-10 05:51:10

问题


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');
    });

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)

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

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


回答1:


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);
 }



回答2:


You could do something like this in your routes:

Route::get('/some-prefix/{any}', function () {
    return abort('404');
})->where('any', '.*');



回答3:


This is what I did (I pick the answer of @Leo_Kelmendi as the correct one, I just want to share my whole code with the one he put too, by the way it has 2 'n' the MethodNotAllowedHttpExceptionn):

public function render($request, Exception $exception)
{
    /*
     * Redirect if token mismatch error
     * Usually because user stayed on the same screen too long and their session expired
     */
    if ($exception instanceof TokenMismatchException) {
        return redirect()->route('frontend.auth.login');
    }

    /*
     * All instances of GeneralException redirect back with a flash message to show a bootstrap alert-error
     */
    if ($exception instanceof GeneralException) {
        return redirect()->back()->withInput()->withFlashDanger($exception->getMessage());
    }

    if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
        return abort('404');
    }

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


来源:https://stackoverflow.com/questions/46836778/methodnotallowedhttpexception-redirect-to-404

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!