2020-03-09
Last week I worked in the
Routing component, which is a file system iterator that allows to describe the application routing based on route objects organized in the file system. Routing works by defining route file object properties in the file system:
[CODE lang="php" title="/api/articles/{id}/RouteDecorator.php"]return new class() extends RouteDecorator
{
public function name(): RouteNameInterface
{
return new RouteName('article-entity');
}
// You can omit this method to use the default regex.
public function wildcards(): RouteWildcardsInterface
{
return new RouteWildcards(
(new RouteWildcard('id'))
->withMatch(new RouteWildcardMatch('\d+'))
);
}
};[/CODE]
In the example above, the path is for
/api/articles/{id}
and it defines the route name (identifier) and the regex matching for digits (meaning that it will resolve
/api/articles/123
but not
/api/articles/alpha
). This piece of code works as a nice example on how granular the framework is.
Route endpoints (method + path) are also defined in a file return object:
[CODE lang="php" title="/api/articles/{id}/Get.php"]return new class() extends RouteEndpoint
{
public function getController(): ControllerInterface
{
return new GetArticleController;
}
};[/CODE]
In the example above, a little magic is used to determine the HTTP method based on the filename and the controller is the only thing you have to declare.
Most likely the API will be ready for my next post here.
WRONG, because reasons. The main problem was that I wanted to use the HTTP OPTIONS method to describe the route endpoint communication with human readable data, like arguments type, purpose of the endpoint, etc. This was crucial to provide the self-described API I've been talking since forever.
Problem is that OPTIONS can't be cached (I know, I forgot to read the last line of the spec) so I can't use the same API to self-describe itself unless I add extra GET paths under the API routes for this purpose which I will hate myself if I do it, and everybody using the framework will also hate. Since that option is out of the table, I'm now building a
/spec/
route which will work as the API that self-describe the whole public application endpoints.
Most likely the API will be ready for my next post here... Deja vu.
Cheers,
Rodolfo.