Laravel 5: 在身份验证后保护删除路由时,MethodNotAllowedHttpException在RouteCollection.php中产生
Laravel 5: 在身份验证后保护删除路由时,MethodNotAllowedHttpException在RouteCollection.php中产生
希望有人能帮忙。
在这个阶段,我已经成功地实现了所有其他方法(编辑和新建)与受保护路由的配合。只有删除的功能不太好用(尽管我相信这只是我在做些愚蠢的事情)。我通过将删除操作从路由组中分离出来以及取消中间件的分配,成功地删除了配置文件(对象),所以这个操作是正确的。我尝试像其他帖子中建议的那样在firebug中检查它,结果得到以下信息:
405 Method Not Allowed
Allow DELETE
Cache-Control no-cache, private
Connection close
Content-Type text/html
Date Wed, 11 Nov 2015 12:36:40 GMT
Host 127.0.0.1:8000
X-Powered-By PHP/5.5.9-1ubuntu4.14
我希望用户在创建新条目或编辑现有条目之前进行身份验证(删除也是同样如此)。
当我尝试通过点击按钮进行删除时,它将我重定向到auth/login(laravel的快速开始认证功能默认的地址),但是在成功登录后,地址栏中的URL是:
127.0.0.1:8000/delete/48
显示:
MethodNotAllowedHttpException in RouteCollection.php line 219:
routes.php:
Route::get('/new', [ 'as' => 'create', 'middleware' => 'auth', 'uses' => 'ConfigsController@getCreate' ]); $router->group(['middleware' => 'auth'], function($router) { Route::get('{id}/edit', 'ConfigsController@edit'); Route::delete('/delete/{id}/', 'ConfigsController@getDL'); }); +--------+----------+-------------------+----------+-------------------------------------------------------+------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+----------+-------------------+----------+-------------------------------------------------------+------------+ | | DELETE | delete/{id} | | App\Http\Controllers\ConfigsController@getDL | auth | | | POST | new | | App\Http\Controllers\ConfigsController@postCreate | | | | GET|HEAD | new | create | App\Http\Controllers\ConfigsController@getCreate | auth | | | GET|HEAD | {id}/edit | | App\Http\Controllers\ConfigsController@edit | auth |
ConfigsController.php:
public function getCreate() { return view('create'); } public function edit($id) { $configs = Config::find($id); return view('edit') ->with('configs', $configs); } public function getDL($id) { $configs = Config::find($id)->delete(); return Redirect::to('/'); }
index.blade.php(编辑和删除)和app.blade.php(新建):
原因:由于Laravel的authenticate()函数默认使用"intended"进行重定向,当用户尚未登录并且在/delete/48页面进行GET请求时,Laravel会强制用户登录,登录成功后将用户重定向到"intended"页面,即/delete/48页面。然而,浏览器只能进行GET或POST请求,无法进行DELETE请求,因此出现了MethodNotAllowedHttpException异常。
解决方法:将DELETE请求改为GET请求即可解决该问题。
文章如下:
在使用Laravel 5进行身份验证时,出现了一个问题:在进行身份验证后保护删除路由时出现了MethodNotAllowedHttpException异常。这是因为Laravel的authenticate()函数默认使用"intended"进行重定向。
具体来说,当用户尚未登录并且在/whateverpage页面进行GET请求时,Laravel会强制用户登录。登录成功后,Laravel将用户重定向到"intended"页面,即我们的例子中的/whateverpage页面,而在你的情况下是/delete/48页面。
这个问题的出现是因为浏览器只能进行GET或POST请求,无法进行DELETE请求。要解决这个问题,可以将DELETE请求改为GET请求。
在解决这个问题的过程中,我们可以通过设置$redirectPath属性来自定义身份验证后重定向的页面。具体的方法可以参考Laravel文档中的相关部分。另外,还需要注意到,HTML表单不支持PUT、PATCH或DELETE方法,所以在定义这些方法的路由时,需要在表单中添加一个隐藏的_method字段来实现伪装。
以下是一个解决这个问题的示例代码:
routes.php文件中的路由定义:
Route::group(['middleware' => 'auth'], function () { Route::group(['prefix' => 'config'], function() { Route::get('create', 'ConfigController'); Route::post('create', 'ConfigController'); Route::get('{id}/edit', 'ConfigController'); Route::post('{id}/edit', 'ConfigController'); Route::get('{id}/delete', ['as' => 'config.delete', 'uses' => 'ConfigController']); }); });
ConfigController.php文件中的控制器代码:
with('config', $config->findOrFail($id)); } public function postEdit($id, Config $config) { // do something return Redirect::to('/'); } public function destroy($id, Config $config) { $config->findOrFail($id)->delete(); return Redirect::to('/'); } }
通过以上代码示例,我们可以看到解决这个问题的关键是将DELETE请求改为GET请求,并且在路由中使用`Route::get('{id}/delete', ['as' => 'config.delete', 'uses' => 'ConfigController']);`来定义删除路由。
最后,我们可以生成一个带有删除按钮的"删除链接",代码如下:
Delete
通过以上解决方法,我们可以成功解决Laravel 5中在进行身份验证后保护删除路由时出现的MethodNotAllowedHttpException异常。