这是一组控制器的列表:
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');
});
我想在输入网址时重定向到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)
我在我的控制器failOrFind
和store
方法中加了一个edit
,所以我可以改写为404路线,如:
http://myapp.com/some-prefix/9999/edit
值9999
不存在,但我怎么能按照我的要求做呢?
答案 0 :(得分:9)
在App\Exception
方法中添加:{/ 1>转到handler.php
打开render()
public function render($request, Exception $exception)
{
if($exception instanceof \Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException){
return abort('404');
}
return parent::render($request, $exception);
}
答案 1 :(得分:4)
您可以在路线中执行以下操作:
Route::get('/some-prefix/{any}', function () {
return abort('404');
})->where('any', '.*');
答案 2 :(得分: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);
}