Give us a second

PATCH/PUT ROUTES

Create routes with PATCH/PUT

Routing with PUT and PATCH

Before you even consider using PATCH and PUT, it is important to understand that PHP by default supports the GET and POST HTTP protocols.

A web form can send via it's method either a GET or a POST request. If AJAX/Fetch is used, then any HTTP method can be used like GET, POST, PUT, PATCH, DELETE, or any other.

Since PHP doesn't know how to retrieve the values/variables passed via any method that is not a GET or a POST, I have decided to respect it and not extend the language to support it. This doesn't mean that you cannot use any other method, it only means that variables passed via a form or as JSON data will not be understood by any other method.

Variables passed via the URL using any HTTP method will, on the other hand, be understood by this routing library.


// Valid route
put('/user/$id', 'comp/update_user.php')
// Variables passed via a form
// or via AJAX as formData
// will not be understood

Other option to PATCH or PUT?

Yes, simply use a POST request. If you really want to create a REST API and must use any other method, then you can extend your own PHP code to support it. Example of a route that will update a user. All the data for the user, including the user's id, will be passed in the form:


// Valid route
post('/update-user' , 'update-user.php');

// HTML form
<form action="update-user" method="POST">
  <input name="user_id" type="text">
  <input name="user_name" type="text">
  <button>Update</button>
</form>