Skip to main content
Github

Experimental HTTP features

HTTP method override

GET and POST are the only HTTP methods supported by HTML forms in web browsers. In such situations, you can simulate other methods using Inphinit\Experimental\Http\Method::override(). See the example below:

use Inphinit\Experimental\Http\Method; Method::override(); // E.g., http://localhost/form $app->action('GET', '/form', function () { echo '<form action="/status" method="POST">'; // Override echo '<input type="hidden" name="_method" value="PUT">'; echo '<input type="text" name="message"><br>'; echo '<button>Update status</button>'; echo '</form>'; }); // E.g., http://localhost/status $app->action('PUT', '/status', function () { echo 'Status updated!<br>'; echo '<a href="/status?_method=DELETE">clear<a>'; }); // E.g., http://localhost/status $app->action('DELETE', '/status', function () { echo 'Status cleared!'; });

Using the method parameter

The _method parameter can be sent in the body of a POST request:

POST /status HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded field1=value1&field2=value2&_method=PUT

Using with HTML:

<form action="/status" method="POST"> <input type="hidden" name="_method" value="PUT"> <input type="text" name="message"><br> <button>Request</button> </form>

Headers

To change the method in GET requests, you must use the x-http-method-override header:

GET /status HTTP/1.1 Host: localhost Content-Type: application/x-www-form-urlencoded x-http-method-override: DELETE

Changing behavior:

By default, only the DELETE, PATCH, PUT, and QUERY methods are accepted, but it is possible to adjust this behavior manually by using:

use Inphinit\Experimental\Http\Method; $override = new Method($_SERVER, $_POST); // Set the allowed methods to FOO and BAR $override->setAllowed(['FOO', 'BAR']); // The header that will contain the method is `x-my-header` $override->setHeader('x-my-header'); // The POST parameter that will contain the method is `_foobar` $override->setParam('_foobar'); $_SERVER['REQUEST_METHOD'] = (string) $override;