简体   繁体   English

OctoberCMS-如何在php部分的函数中访问组件的属性?

[英]OctoberCMS - How to access properties of a component in a function inside of the php section?

Is it possible to access the properties of a component eg blogPosts in the OnStart() function? 是否可以在OnStart()函数中访问组件的属性,例如blogPosts? If so, how? 如果是这样,怎么办?

title = "Blog Category"
url = "/blog/category/:slug/:page?"

[blogPosts]
categoryFilter = ":slug"
postsPerPage = 3
==
function onStart()
{
    // Access categoryFilter property
    $catfilter = ???
    ...
}
==

I got to ask why do you need to access the component properties? 我要问为什么您需要访问组件属性?

You can access the parameter ':slug' $this->param('slug'); 您可以访问参数':slug' $this->param('slug'); .

This should work for you to get the array of properties. 这应该对您有用,以获得属性数组。

function onStart() {
    $this['properties'] = collect($this->page->components['blogPosts'])->last()['postsPerPage'];
}

I am adding another way to get the properties from the array. 我正在添加另一种从数组中获取属性的方法。 I am assuming that properties is probably always last but if it isn't: 我假设属性可能总是最后一个,但如果不是这样:

    $array = collect($this->page->components['blogPosts'])->toArray();
    $properties = [];
    foreach ( $array as $key => $property) {
        if ( strpos($key, 'properties') !== false ) {
            $properties[$key] = $property;
        }
    }
    $this['properties'] = $properties['postsPerPage'];

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

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