Registration
The endpoint resolves the component id in the token through the component
registry (service('componentRegistry')). Ids are lowercase
(a-z0-9_.:-, at most 100 characters); class names never leave the server.
| Where | Id | How |
|---|---|---|
| Page | <panel>:<page slug>:<kind> |
the page implements Anvil\Page\HasComponents and returns the component from component(string $kind), null for unknown kinds |
| Resource detail page | <panel>:<resource slug>:<kind> |
Resource::detailComponents(), see Components on the detail page |
| Anywhere else | any id | service('componentRegistry')->register('projects.title', static fn () => new ProjectTitle()) |
- Page and resource components are found without registration: the
registry asks the panels for ids of the form
<panel>:<slug>:<kind>. - The registry lives for one request. Components registered with
register()must be registered on every request, not only while the page is rendered; see Making it reachable. Registering an id twice throws aLogicException. component()of a page receives only the kind. One page can provide several components (Anvil\Access\ProfilePagehaspasswordandmfa).
Registering in Events.php
Section titled “Registering in Events.php”Components that are not part of a page or resource, e.g. a widget used on several pages, are registered in an event that runs on every request:
use App\Components\ProjectTitle;use CodeIgniter\Events\Events;
Events::on('post_controller_constructor', static function (): void { service('componentRegistry')->register('admin:projects.title', static fn () => new ProjectTitle());});Start the id with the panel (admin:) when the component belongs to one
panel; the token binds it to that panel anyway, and the test helper
dispatchComponent() expects the prefix.
Which way to choose
Section titled “Which way to choose”| Situation | Registration |
|---|---|
| a page of your own with interactive parts | HasComponents on the page |
| a section on the detail page of a record | Resource::detailComponents() |
| a row, record or bulk operation | no component: an action |
| one component used on several pages | componentRegistry()->register() |