Give us a second

DELETE ROUTES

Create routes with DELETE

Routing with DELETE

Creating a route that will delete a resource, item, product, user, and so on, is really simple.

Consider two options:

1. Using the DELETE HTTP method

2. Using the POST HTTP method

1. Routing with DELETE

In this type of routes, the only thing the end-point needs is the unique identifier of a resource. This unique identifier will be used to delete the entry in the database. No other variables should be passed to the backend. If a variable or multiple variables need to be passed, then it will be done in the URL. Also, the DELETE protocol doesn't cache the data.


delete('/item/$id' , 'delete-item.php');

2. Routing with POST to DELETE

Another option to DELETE is to use POST. The advantage of this approach is that variables can be passed in the URL as well as formData or JSON payload and, as with DELETE, the data will not be cached. The disadvantage is that, in theory, it will not be a REST API.


post('/delete-item/$id' , 'delete.php');

Which option to choose from?

It is up to you. There is no right or wrong answer. If you want to create a fully compliant REST API, use the DELETE method to create your routes. If you need to pass formData or JSON payload to delete a resource, use POST.

As a side note, many applications to maintain simplicity, use only GET and POST because that is the only thing web forms understand.