2017-10-19 146 views
0

这是一组控制器的名单:MethodNotAllowedHttpException,重定向到404

Route::group([ 
     'prefix'  => 'some-prefix', 
    ], function() { 
     Route::get('/', '[email protected]')->name('some-prefix'); 
     Route::post('/get', '[email protected]')->name('some-prefix.get'); 
     Route::get('/getall/{type}', '[email protected]')->name('some-prefix.getall'); 
     Route::get('/create', '[email protected]')->name('some-prefix.create'); 
     Route::post('/', '[email protected]')->name('some-prefix.store'); 
     Route::get('/edit', '[email protected]')->name('some-prefix.edit'); 
     Route::get('/{id}/edit/', '[email protected]')->name('some-prefix.edit'); 
     Route::put('/{id}', '[email protected]')->name('some-prefix.update'); 
     Route::get('/cambiarestado/{id}', '[email protected]')->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) 

我把我的failOrFindstoreedit种方法在我的控制器,这样我就可以redict到404条路线,如:

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

其中值9999不存在,但我怎么能做到什么,我问?

+0

什么是@ myController的更新和@ myController的编辑的代码?由于这些接受一个动态参数,你应该在那里处理404。 – Devon

回答

2

转到App\Exceptionrender()方法添加开拓handler.php

public function render($request, Exception $exception) 
    { 
     if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpExceptionn){ 
      return abort('404'); 
      } 
     return parent::render($request, $exception); 
    } 
+0

我加入了一些新的生产线也在那里,让我们来看看。我有一些代码,应改变... – pmirnd

+1

@leo请编辑代码'MethodNotAllowedHttpException'不'MethodNotAllowedHttpExceptionn',注意在最后两个“N”。 –

1

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

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

谢谢,我是想这样的事情也没有成功。我会选这一个为正确的,因为我喜欢触摸路由代码要比处理程序,但问题是,我有类似的航线多组...(我对糟糕的代码设计的错,我应该用'resource'取而代之,发布,放等) – pmirnd

0

这是我做的(我捡的答案@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); 
}