【问题标题】:MethodNotAllowedHttpException, redirect to 404MethodNotAllowedHttpException,重定向到 404
【发布时间】:2018-03-31 21:32:56
【问题描述】:

这是一组控制器的列表:

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 错误:

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

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

(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)

我在我的控制器中的storeedit 方法中放置了一个failOrFind,这样我就可以重定向到404 条路由,例如:

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

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

【问题讨论】:

  • MyController@update 和 MyController@edit 的代码是什么?由于这些接受动态参数,因此您应该在那里处理 404。

标签: php laravel routes


【解决方案1】:

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

【讨论】:

  • 我也在那里添加了一些新行,让我们看看。我有一些应该更改的代码...
  • @leo 请编辑您的代码MethodNotAllowedHttpException 而不是MethodNotAllowedHttpExceptionn,注意最后两个“n”。
【解决方案2】:

你可以在你的路线中做这样的事情:

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

【讨论】:

  • 谢谢,我也在尝试类似的方法,但没有成功。我会选择这个作为正确的,因为我更喜欢接触 Routes 代码而不是 Handler,但问题是我有很多类似的路由组......(我的错误代码设计错误,我应该使用 resource而是获取、发布、放置等)
【解决方案3】:

这就是我所做的(我选择@Leo_Kelmendi 的答案作为正确的答案,我只想与他也提供的那个分享我的整个代码,顺便说一下它有2'n'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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多