gpt4 book ai didi

php - MethodNotAllowedHttpException,重定向到 404

转载 作者:行者123 更新时间:2023-12-04 23:33:56 29 4
gpt4 key购买 nike

这是一组 Controller 的列表:

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)

我放了一个 failOrFind在我的 store 里面和 edit Controller 中的方法,因此我可以将其还原为 404 路由,例如:
http://myapp.com/some-prefix/9999/edit

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

最佳答案

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

关于php - MethodNotAllowedHttpException,重定向到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46836778/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com