-
Notifications
You must be signed in to change notification settings - Fork 0
Agent
The Agent object is injected into your controller's __onInit() and __onLoad() hooks. Use it to register routes, events, API commands, bridge commands, and configure module behaviour.
Register a URL route pattern mapped to a controller closure file. The framework matches the URL left-to-right against registered patterns.
| Token | Matches | Example |
| --- | --- | --- |
| :d | Digits only | /user/:d → /user/42 |
| :a | Any non-slash characters | /post/:a → /post/abc-123 |
| :w | Word characters (a-zA-Z, 0-9, _) | /tag/:w → /tag/my_tag |
| :W | Non-word characters | /sep/:W → /sep/--.. |
| :D | Non-digit characters | /code/:D → /code/abc |
| :[regex] | Custom character class | :[a-z0-9-] → custom match |
| {min,max} | Length constraint | :a{3,50} → 3-50 non-slash characters |
// Single route
$agent->addRoute('/', 'index');
$agent->addRoute('/user/:d', 'showUser');
$agent->addRoute('/post/:a{3,50}', 'showPost');
// Array syntax for multiple routes
$agent->addRoute([
'/' => 'index',
'/create' => 'createForm',
'/edit/:d' => 'editForm',
'/file/:*' => 'serveFile',
]);📝 Note: Closure files: The
$pathmaps to a PHP file incontroller/. E.g.'showUser'→controller/{module_code}.showUser.php.
Register a lazily-resolved route. The handler file is only loaded when the route is actually matched.
Supports nested arrays to define hierarchical routes where each level corresponds to a URL path segment and folder level. Use @self as a key to map the current directory level itself.
HTTP method prefixes (e.g. POST, GET|POST) can appear at any nesting level. A method on a parent key is inherited by all children unless overridden:
// Flat registration
$agent->addLazyRoute('GET /users', 'list');
// Nested — all children inherit POST
$agent->addLazyRoute(['POST api' => [
'fetch' => 'fetch', // POST api/fetch
'add' => 'add', // POST api/add
]]);
// Nested — per-leaf methods
$agent->addLazyRoute(['api' => [
'fetch' => 'fetch', // Any method
'POST add' => 'add', // POST only
'POST delete' => 'delete', // POST only
]]);
// Override inherited method
$agent->addLazyRoute(['GET api' => [
'list' => 'list', // GET (inherited)
'POST create' => 'create', // POST (overridden)
]]);💡 Tip: Use for: Large modules with many routes where loading all handlers upfront would hurt startup performance.
Redirect a matched route to another module's handler. The URL stays the same but processing is delegated.
// Delegate /admin routes to admin_panel module
$agent->addShadowRoute('/admin', 'admin_panel', 'dashboard');Add a script-mode route. Scripts execute after normal routing and can produce additional output (e.g. analytics, footers).
Register event listener(s). When another module fires the event via trigger(), your callback runs.
// Single event
$agent->listen('user.login', 'handleUserLogin');
// Multiple events
$agent->listen([
'user.login' => 'handleLogin',
'user.logout' => 'handleLogout',
'order.placed' => 'handleOrder',
]);Register a command that other modules can call via $this->api('your_module')->commandName().
$agent->addAPICommand('getUser', 'apiGetUser');
$agent->addAPICommand('validateEmail', 'apiValidateEmail');
// Array syntax
$agent->addAPICommand([
'getUser' => 'apiGetUser',
'updateProfile' => 'apiUpdateProfile',
]);Register commands for cross-distributor (cross-domain) communication via the internal bridge.
Register a callback to execute when a specific module becomes ready. Useful when your module depends on another module's initialization.
$agent->await('auth_module', function(ModuleInfo $authInfo) {
// auth_module is now ready, safe to use its API
});Get the module's ThreadManager for spawning background processes. See Thread & ThreadManager.