【问题标题】:In CakePHP, What does ComponentRegistry do?在 CakePHP 中,ComponentRegistry 是做什么的?
【发布时间】:2019-07-23 20:19:08
【问题描述】:

我创建了一个组件,当我看到该组件的测试代码时,创建了骨架代码:

public function setUp()
    {
        parent::setUp();
        $registry = new ComponentRegistry();
        $this->Precio = new PrecioComponent($registry);
    }

¿ ComponentRegistry 对象 ($registry) 有什么用?

【问题讨论】:

    标签: cakephp cakephp-3.x


    【解决方案1】:

    顾名思义,它是一个注册组件的对象,即组件实例的集合。在您的应用程序中,它是持有a component registry instance 的控制器,它使用load and instantiate components 的注册表,这就是组件通过其构造函数接收对注册表的引用的地方。

    组件使用注册表来访问它们所附加的控制器,并自行加载组件,这是必需的依赖项。

    所以生成的测试是测试组件所需的最低要求,例如,如果需要,您可以通过其构造函数将控制器实例传递给组件注册表,以便组件中可能使用的 $this->_registry->getController() 确实返回一个控制器:

    $controller = new \Cake\Controller\Controller();
    $registry = new ComponentRegistry($controller);
    $this->Precio = new PrecioComponent($registry);
    

    另见

    【讨论】:

      猜你喜欢
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-23
      • 2014-01-13
      • 2011-12-27
      • 2010-10-01
      • 2017-09-12
      相关资源
      最近更新 更多