About Documentation Github

Routing API Reference

Routing, rerouting and dispatching. See Routing Requests for some examples.

route($path, $resolver)

Add a route with the specified $path and $resolver function. $path can contain parameters, specified by a single word in {braces}. E.g. /blog/{keyword}. Each parameter is passed to the resolver function as its own argument.

If a resolver returns false, Pagenode will search and execute a 404 handler (a route with the path /*) or execute its own 404 handler.

reroute($sourcePath, $targetPath)

Internally re-route the $sourcePath to $targetPath.

Matching parameters can be passed on to the target URL by specifying the parameter index in braces. Use {1} for the first parameter. E.g.:

reroute('/legacy-url/blog-post-{id}-{keyword}', '/blog/{2}');

This will re-route /legacy-url/blog-post-42-pagenode to /blog/pagenode.

redirect($path, [$params])

Convenience function to redirect a user via header('Location: …'). The optional $params will be appended to the path as URL query parameters. This function immediately exits the current script. E.g.:

route('/legacy-url/blog-post-{id}-{keyword}', function($id, $kw){
    redirect('/blog/'.$kw, ['from' => $id]);

This will redirect /legacy-url/blog-post-42-pagenode to /blog/pagenode?from=42.


Tests all routes added by route() with the specified $request until a matching route is found and the resolver is called.

If no $request is specified, the request is automatically determined from the $_SERVER['REQUEST_URI'].

© 2024 Dominic Szablewski – rendered in (6.11ms)