nette/component-model
Nette Component Model is a lightweight PHP package for building component-based UI structures. It provides component containers, naming and lookup, lifecycle hooks, and signal handling—forming the foundation used by Nette for reusable, composable components.
Install via Composer: composer require nette/component-model. This package delivers a minimal, standalone component model adapted from Nette Framework—ideal for building hierarchical, reusable objects (e.g., UI widgets, domain models with parent-child semantics). Start by extending Component and using Container to manage children. Minimal setup example:
use Nette\ComponentModel\Component;
use Nette\ComponentModel\Container;
class Alert extends Component
{
public function __construct(string $message)
{
$this->monitor(Component::class, fn($c, $p) => $this->message = $message);
$this->message = $message;
}
public function render(): void
{
echo "<div>{$this->message}</div>";
}
}
$container = new Container;
$container->addComponent(new Alert('Hello'), 'alert');
$container['alert']->render(); // Using ArrayAccess
First reference: src/ComponentModel/Component.php and src/ComponentModel/Container.php. Core methods (addComponent, getComponent, removeComponent) and lifecycle hooks (monitor) are the primary API surface.
Component as your base class. Use getComponent($name) internally to access children—this auto-handles discovery and lazy initialization.monitor($classNameOrTrait, callable) over legacy attached()/detached() (deprecated since v2.4). Callbacks receive ($component, ?Component $parent) and fire only on state change.Container to build component trees (e.g., form controls, widget groups). Support both named ($container->getComponent('submit')) and array-style ($container['submit']) access via ArrayAccess trait.Container::getComponentTree() (v3.1.0+) to serialize component hierarchies—ideal for admin dashboards or middleware logs.new Container(name: 'main')), typed properties in subclasses, and strict declare(strict_types=1) in your codebase.Component has no constructor in v3.x. If you override __construct, do not pass $parent or $name—they are auto-assigned via Component. Passing them manually triggers deprecation warnings (v2.2+).Container::getComponent() requires string names (v3.0+). Using integers throws NotFoundException, but ArrayAccess accepts 123 as key. Prefer string names like 'btn_submit' over numeric IDs.getComponents($deep = false) now returns always an array (not void when $deep omitted) and $deep is silently deprecated. Prefer getComponentTree() for introspection.NotFoundException from getComponent() includes suggestions for typoed names (e.g., “Component 'submt' not found. Did you mean 'submit'?”). Log these for dev feedback.$this->$key = $val) in monitor() callbacks—PHP 8.5 deprecates them. Use typed properties or #[AllowDynamicProperties] only when necessary.How can I help you explore Laravel packages today?