【问题标题】:Access partial's components in PHP section in OctoberCMS在 OctoberCMS 的 PHP 部分中访问部分组件
【发布时间】:2020-10-06 11:24:05
【问题描述】:

我需要访问和修改我在其自己的 PHP 部分中部分加载的组件。

通常我在页面中加载组件,我可以在 PHP 部分中使用 $this->page->components['exampleComponent'] 访问它们。 但是当组件被部分加载时,它就不存在了。

我尝试过 onStart 和 onEnd 来测试它是否是由于十月的生命周期,但它们从未添加到页面组件中。

那我该如何访问呢?

description = "Sample Partial"

[someComponent exampleComponent]
code = "header-menu"

==
<?php
function onStart(){

   // Need to access partial's component here
   $exampleComponent = ??? 
}
?>
==

【问题讨论】:

    标签: octobercms


    【解决方案1】:

    您可以使用controller object 来管理partial stack

    在文档中,他们提到部分具有不同的life-cycle then pages,所以这可能是它的组件不在page 对象中的原因

    控制器findComponentByName('component-name')有一个方便的方法从page or partial搜索组件

    所以你可以使用below code 来获取你的组件。

    description = "Sample Partial"
    
    [someComponent exampleComponent]
    code = "header-menu"
    
    ==
    <?php
    function onStart(){
       // Need to access partial's component here 
       $exampleComponent = $this->controller->findComponentByName('exampleComponent');
       //           USE controller obj ^ and this method ^
    }
    ?>
    

    它应该可以工作。

    如有疑问请评论。

    【讨论】:

    • 谢谢哈迪克,很好的回答...我不知道我可以那样做。像魅力一样工作。
    • 当然,我很高兴它帮助了你:)
    猜你喜欢
    • 2019-11-28
    • 2017-07-08
    • 2017-10-18
    • 2016-09-13
    • 2022-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多