How to autowire ( autowiring ) / get services in Nette from the di dependency injection container context

430 views Asked by At

How to autowire nette services from the container, to be accessible to be used inside presenters ( controllers ) or models ,etc?

1

There are 1 answers

0
FantomX1 On

To autowire services in Nette to be easily retrievable, used, they must be registered in a config.neon config, or must be those preset by default by framework, in multiple ways:

  • specifying and passing by interface, class type in the constructor
    use App\Model; // or use App\Model\ArticleRepository;
    public function __construct(
        Model\ArticlesRepository $articlesRepository,
     ...
    )
    {
        $this->articlesRepository = $articlesRepository;
  • autowiring via a @inject tag attribute annotation
    /** @var  \Nette\Http\Request @inject */ // however , this one is just example, the request service is already available in the presenters via $this->request attribute
    public $request;

the list of all available services including those configured in configs, also along those default predefined can be seen at the same place at the Nette Tracy Debugger bar -> DIC (dependency injection container) button

enter image description here