REST
On this page
Controllers are RESTful (Representational State Transfer) and can distinguish between different HTTP methods.
Restricting HTTP Methods
By using the name of the desired HTTP method as an additional prefix for a do method, you can restrict the HTTP methods via which this controller method can be invoked. For example, if a controller is called via GET with the command path /foo, it first looks for the method getDoFoo() and, if it does not exist, for doFoo().
class ExampleController extends ControllerAdapter {
public function getDoFoo() {
// GET
}
public function putDoFoo() {
// PUT
}
public function doFoo() {
// POST, DELETE
}
}
Assuming the command path /foo, in this example getDoFoo() can only be invoked via GET, putDoFoo() only via PUT and doFoo() only via POST and DELETE.
For all other controller methods, the allowed HTTP methods can be defined using the attributes n2n\web\http\attribute\Get, n2n\web\http\attribute\Put, n2n\web\http\attribute\Post and n2n\web\http\attribute\Delete. Via ControllerAdapter::getRequest()->getMethod() you can also query the HTTP method of the current request.
<?php
namespace atusch\controller;
use n2n\web\http\controller\ControllerAdapter;
use n2n\web\http\attribute\Get;
use n2n\web\http\attribute\Put;
use n2n\web\http\Method;
class ExampleController extends ControllerAdapter {
#[Get, Put]
public function doFoo($param) {
switch ($this->getRequest()->getMethod()) {
case Method::GET:
echo 'get: ' . $param;
break;
case Method::PUT:
echo 'put: ' . $param;
break;
}
}
}
Restricting Accepted Content Types
With the attribute n2n\web\http\attribute\Consums you can restrict a controller method to requests whose Accept header matches at least one of the specified MIME types. Wildcards and quality values in the Accept header are taken into account (e.g. application/* or */*;q=0.8 match application/json).
<?php
namespace atusch\controller;
use n2n\web\http\controller\ControllerAdapter;
use n2n\web\http\attribute\PathPattern;
use n2n\web\http\attribute\Get;
use n2n\web\http\attribute\Consums;
class ArticleController extends ControllerAdapter {
#[PathPattern('id:#^[0-9]+$#'), Get, Consums('application/json')]
public function getJson(int $id) {
// e.g. output the article as JSON
}
#[PathPattern('id:#^[0-9]+$#'), Get]
public function getHtml(int $id) {
// e.g. forward to an html view
}
}
If the client requests /123 with Accept: application/json, getJson() is invoked. If the Accept header does not match, getJson() is skipped and the next matching method, here getHtml(), is invoked instead. Like this you can deliver different representations of the same resource for the same path.
Query Parameters
By declaring parameters of your controller method with the types below, you can have corresponding values from the URL or post query passed to you.
| Parameter type | Source | HTTP methods |
|---|---|---|
n2n\web\http\controller\ParamQuery | URL query | all |
n2n\web\http\controller\ParamGet | URL query | GET |
n2n\web\http\controller\ParamPut | URL query | PUT |
n2n\web\http\controller\ParamDelete | URL query | DELETE |
n2n\web\http\controller\ParamPost | post query | POST |
These parameter types can also be used to restrict the HTTP methods via which a controller method can be invoked. For example, if you define a mandatory parameter of type ParamGet for a controller method, it can only be invoked via GET requests.
Example 1
public function doFoo(ParamGet $param) {
// code
}
doFoo() can only be invoked via GET.
Example 2
public function doFoo(ParamGet $param = null) {
// code
}
doFoo() can be invoked via all HTTP methods. $param is only not null for GET requests with a matching URL query.
Example 3
public function doFoo(ParamQuery $param, ParamPost $param2) {
// code
}
doFoo() can only be invoked via POST. The request requires a parameter "param" in the URL query as well as a parameter "param2" in the post query.