Change home on plugin side

I cant change home option from plugin like that:

Kirby::plugin('afbora/test', [
    'options' => [
        'home' => "homeThree",
    ]
]);

Just plugin home option didnt work and i tried to get from config.php like that:

return [
    'home' => option('afbora.test.home'),
];

Getting error:

Fatal error : Out of memory (allocated 299892736) (tried to allocate 65536 bytes)

The config is loaded before the plugins, otherwise the plugins could not use the options. So setting an option from a plugin in the config is not possible.

Hmm so how can i override config.php options? Any alternate ways?

What’s your use case for setting the homepage from a plugin?

I’m trying to do multipurpose app that have multiple homepages like that:

  • /homepages
    • /home01
    • /home03
    • /home04

When user change/select any homepage, i want to show selected homepage as home. I just want to change home option as dynamic.

I solved this issue for now with layouts with blade feature:

The alternative would be to return the selected page via a route that listens to the ‘/’ (and in case of subpages home/(:any)/home/(:all)) pattern.

Other idea:

Store the page id in a text file via a hook and the read it in the config:

'home' => file_get_contents(__DIR__.'/../../homepage.txt'),

// or with a fallback if file doesn't exist
'home' => ($string = F::read(__DIR__.'/../../log.txt')) ? $string:  'home',

(Just trying to get creative)

Or take a field e.g. in the “site.txt” or the “home.txt” content file and store there the name or path of the actual home page. In the template you can use this field as normal…

@byybora In fact, for your use case where the homepages are subfolders of the homepage, your Blade solution (I guess using partials, I’m not familiar with Blade), actually makes much more sense then the option variant. Without blade, you could achieve the same with snippets or by simply rendering a page with a different template in the home.php template.

<?php
echo page('somepage')->render();

If you use the home option, however, to choose another home page, the actual homepage would still exist and could be accessed like normal.

So the use cases are different.

1 Like

You right, i can do with snippet. And I liked page render() solution. I’ll try.

I loved Blade template engine and used layouts. With Blade, template system is more useful, practical and coding fun. I think, you’ll love Blade :heart:

I suggest you take a look blade file:

Thank you suggestions @texnixe and @HeinerEF

Be careful though not to render a page into it itself.

The render method can also be used in a route like this:

    'routes' => [
        [
            'pattern' => '/',
            'action'  => function() {
                if ($page = site()->homepageselector()->toPage()) {
                   return $page->render();
                }
                        
            }
        ]
    ]