0

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

FantomX1
  • 1,577
  • 2
  • 15
  • 23

1 Answers1

0

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

FantomX1
  • 1,577
  • 2
  • 15
  • 23