简体   繁体   English

如何从 di 依赖注入容器上下文在 nette 中自动装配/自动装配服务

[英]How to autowire / autowiring services in nette from the di dependency injection container context

How to autowire nette services from the container, to be accessible to be used inside presenters ( controllers ) or models,etc?如何从容器中自动连接网络服务,以便在演示者(控制器)或模型等内部使用?

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:要在 Nette 中自动装配服务以便轻松检索和使用,它们必须在 config.neon 配置中注册,或者必须是框架默认预设的,有多种方式:

  • specifying and passing by interface, class type in the constructor通过接口指定和传递,class 类型在构造函数中
    use App\Model; // or use App\Model\ArticleRepository;
    public function __construct(
        Model\ArticlesRepository $articlesRepository,
     ...
    )
    {
        $this->articlesRepository = $articlesRepository;
  • autowiring via a @inject tag attribute annotation通过@inject 标签属性注解自动装配
    /** @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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM