方法PUT和DELETE在Laravel中不起作用

时间:2016-09-06 19:22:51

标签: laravel laravel-5 laravel-routing put http-delete

我在Laravel的路线有问题。

当我使用以下路线时:

Route::resource('user', 'PostsController');

方法PUT和DELETE在我的控制器中使用destroy和update功能正常工作,但是当我使用以下路径时:

Route::delete('{$id}', function($id){
    return "Remover o usuário ".$id;
});

Route::put('{$id}', function($id){
    return "Editar o usuário ".$id;
});

我有以下错误:

NotFoundHttpException in RouteCollection.php line 161:

in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 780
at Router->findRoute(object(Request)) in Router.php line 610
at Router->dispatchToRoute(object(Request)) in Router.php line 596
at Router->dispatch(object(Request)) in Kernel.php line 267
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in HandleCors.php line 42
at HandleCors->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
at Pipeline->then(object(Closure)) in Kernel.php line 149
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
at Kernel->handle(object(Request)) in index.php line 56
at require_once('C:\xampp\htdocs\apiRest\public\index.php') in server.php line 21

Bellow按照图片了解我发送请求的详细信息

Request

我正在使用邮差发送请求。

拜托,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

在Laravel中,路线参数的定义没有$,而不是{id},而不是{$id}。有关详细信息,请参阅docs

所以在你的情况下:

Route::delete('{id}', function($id){
    return "Remover o usuário ".$id;
});

Route::put('{id}', function($id){
    return "Editar o usuário ".$id;
});