Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Component Model Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Component Inheritance: Extend Component as your base class. Use getComponent($name) internally to access children—this auto-handles discovery and lazy initialization.
  • Lifecycle Monitoring: Prefer monitor($classNameOrTrait, callable) over legacy attached()/detached() (deprecated since v2.4). Callbacks receive ($component, ?Component $parent) and fire only on state change.
  • Container as Hierarchical Registry: Use 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.
  • Component Trees for Debugging: Use Container::getComponentTree() (v3.1.0+) to serialize component hierarchies—ideal for admin dashboards or middleware logs.
  • Strict Typing Integration: Leverage PHP 8.2+ features: named arguments (new Container(name: 'main')), typed properties in subclasses, and strict declare(strict_types=1) in your codebase.

Gotchas and Tips

  • Component Constructor Changes: 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+).
  • Name Encoding Quirks: 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.
  • Deprecation in v3.1.0: getComponents($deep = false) now returns always an array (not void when $deep omitted) and $deep is silently deprecated. Prefer getComponentTree() for introspection.
  • Exception Messages: NotFoundException from getComponent() includes suggestions for typoed names (e.g., “Component 'submt' not found. Did you mean 'submit'?”). Log these for dev feedback.
  • PHP 8.5 Compatibility: Avoid dynamic properties ($this->$key = $val) in monitor() callbacks—PHP 8.5 deprecates them. Use typed properties or #[AllowDynamicProperties] only when necessary.
  • No Auto-Wiring: Unlike full Nette DI, this package offers no automatic dependency injection or config-based wiring. You must attach components explicitly.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport