This site is built with Viewi itself. It is experimental and still in development. If you see any bugs please do not hesitate and open an issue or DM me on Twitter.

Dependency Injection


Dependency Injection (DI) is a design pattern that allows you to inject automatically created services (or objects, etc.) without creating them manually. Viewi handles DI automatically by following these rules:

  • Transient. Each component (class that is derived from BaseComponent) will be created each time you need it. Which means, every time you use your component as a tag (<MyComponent />) Viewi will create instance of that component for you.
  • Singleton. Everything else will be considered as a Service class and will be resolved only once and will be reused every time you inject it.

All other DI rules are planned for future development. And you can always request it as feature on our github page so we will know how important it is for you.

How to inject


  • For components (based on BaseComponent) inject your services inside of __init function.

    namespace Application\Components\Views\Demo\ServicesAndModels;
    use Application\Components\Services\Demo\CounterState;
    use Viewi\BaseComponent;
    class ServicesExample extends BaseComponent
        public CounterState $counter;
        public function __init(CounterState $counterState)
            $this->counter = $counterState;

  • For all other services you can inject your dependencies inside of __construct.

    namespace Components\Services;
    use Viewi\Common\ClientRouter;
    use Viewi\Common\HttpClient;
    class AuthService
        private HttpClient $http;
        private ClientRouter $router;
        public function __construct(HttpClient $httpClient, ClientRouter $clientRouter)
            $this->http = $httpClient;
            $this->router = $clientRouter;