-
-
Notifications
You must be signed in to change notification settings - Fork 0
Attribute configuration
Router callbacks are ordinary methods on a class that extends BaseRouter. A method becomes routable when we add one of the route attributes.
The library provides an attribute for each HTTP method:
#[Get]#[Head]#[Post]#[Put]#[Patch]#[Delete]#[Connect]#[Options]#[Trace]#[Any]
Underneath, these all extend HttpRouteMethod, which extends HttpRoute.
Each method attribute accepts the same optional arguments:
pathfunctionnameaccept
Example:
use GT\Routing\BaseRouter;
use GT\Routing\Method\Get;
class AppRouter extends BaseRouter {
#[Get(
path: "/article",
name: "article-list",
accept: "text/html,application/xhtml+xml"
)]
public function articleList():void {
$this->addToViewAssembly("page/article/index.html");
}
}path is matched against the request URI path.
At present, callback path matching is exact:
#[Get(path: "/about")]If we need more involved filesystem-style matching, that is usually handled through PathMatcher and DynamicPath inside the callback rather than in the attribute itself.
accept declares which media types the callback is prepared to serve.
#[Get(path: "/feed", accept: "application/json,application/xml")]If several callbacks match the same request path, the best match is chosen from the request's Accept header. This is covered in more detail in content negotiation.
name gives the route a human-readable identifier. This library does not currently use it for reverse routing, but it can still be useful when keeping a large router organised.
function is accepted by the attribute constructors for compatibility with the wider WebEngine routing model, where the same route metadata may be used alongside logic methods or function-based handlers.
In this repository's callback execution flow, the reflected router method itself is what gets called.
Router callbacks are invoked through GT\ServiceContainer\Injector, so parameters can be resolved from the container.
use GT\Routing\Method\Any;
use GT\Routing\Path\PathMatcher;
use GT\Http\Request;
#[Any]
public function page(Request $request, PathMatcher $pathMatcher):void {
// ...
}That means the callback can stay focused on routing decisions rather than container lookups.
#[Any]
public function fallback():void {
}#[Post(path: "/login")]
public function login():void {
}#[Get(path: "/report", accept: "text/html")]
public function htmlReport():void {
}
#[Get(path: "/report", accept: "application/json")]
public function jsonReport():void {
}Once a callback matches, it will usually add files to the logic or view list. Read assemblies next.
PHP.GT/Routing is a separately maintained component used by PHP.GT/WebEngine.