Skip to content

Attribute configuration

Greg Bowler edited this page Apr 14, 2026 · 1 revision

Router callbacks are ordinary methods on a class that extends BaseRouter. A method becomes routable when we add one of the route attributes.

Available 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.

Supported attribute arguments

Each method attribute accepts the same optional arguments:

  • path
  • function
  • name
  • accept

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");
	}
}

What each argument means

path

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

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

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

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.

Dependency injection in callbacks

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.

Matching examples

Match any request

#[Any]
public function fallback():void {
}

Match one method and one path

#[Post(path: "/login")]
public function login():void {
}

Match by content type

#[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.

Clone this wiki locally