Laravel: unable to create valid DELETE request

Unable to create Delete action in Laravel.

I get Not Found or Token mismatch errors all the time.

My controller:

 class TranslationController extends Controller { public function destroy($id) { //$id = 1; /*$translation = Translation::find($id); $translation->delete();*/ } .... } 

Ajax call:

 /* Delete given translation */ var url = "translation"; var id = 1; $.ajax({ method: 'DELETE', url: url + '/' + id, // data: {'id': id, '_token': token}, success: function() { } }); 

This will give: TokenMismatchException in VerifyCsrfToken.php line 53:

If I try:

 url: url + '/' + id, data: {'_token': token}, // token is equal to csrf_token 

I have: NotFoundHttpException in Controller.php line 269:

Routes

 Route::controller('translation', 'TranslationController'); 

Otherwise, I use the default middleware Laravel 5, I have not changed anything related to csrf.

+6
source share
4 answers

NotFoundHttpException means that a route route for a specific request with a specific HTTP verb is not specified or an action (that is, a controller method) that maps to a verb for a route is erroneously implemented.

Since you mentioned in the post that the TranslationController is defined as an implicit controller ,

 Route::controller('translation', 'TranslationController'); 

and from the controller code that you posted, it’s clear that you did not define a verb for the destroy method in your TranslationController .

If you create php artisan route:list in the root directory of projects with the terminal / command line interface, you will see a list of registered HTTP verbs, mapping to the corresponding URIs and actions.

To define a specific method in an implicit controller, the verb ( GET , PUT , POST , DELETE ) must precede the actual function name. Make sure the destroy method looks like this in your controller:

 public function deleteDestroy($id){ //delete logic for the resource } 

Note: Laravel by default requires that the csrf token csrf passed along with a specific RESTful request, so do not remove data: {'_token': token} from your AJAX call.

Update

Forgot to mention that the url in your AJAX call also needs to be changed as follows in order to work, because this is how the implicit Laravel controllers determine the route for the DELETE request:

 var url = "translation/destroy"; 
+4
source

Here is the spoofing documentation. You need to send a POST ajax request with the _method field set to DELETE

 $.ajax({ method: 'POST', url: url + '/' + id, data: { 'id': id, '_token': token, '_method' : 'DELETE' }, success: function() { } }); 
+2
source

You can try to determine the route this way

 Route::delete('translation/{id}',array('uses' => ' TranslationController@destroy ')); 

In this case, your AJAX will not change. But if you want to keep this route

 Route::controller('translation', 'TranslationController'); 

You should change your Ajax request to:

 /* Delete given translation */ var url = "translation/destroy"; // You must specify the action var id = 1; $.ajax({ method: 'DELETE', url: url + '/' + id, data: {'_token': token}, success: function() { } }); 
+1
source

You must send the token through the header. (especially in version 5.2)

  $.ajax({ type: "post", url: "/routeurl", headers: { 'X-CSRF-Token': "{!! csrf_field() !!}" }, success: function(msg){ // msg } }); 
0
source

All Articles