Does the Site have a model, too?

I’d like to use a modell for the site itself, is that possible? I’d like to overwrite some functions like with Page Models, but in the Site.

No, there is no site model. However, you can create a custom App (aka Kirby) class, in which you redefine the setSite() and site() methods to use your custom Site class that extends the Site class.

In index.php

<?php

require __DIR__ . '/kirby/bootstrap.php';
// require custom Kirby class file
require __DIR__ . '/site/plugins/site-model/src/CustomKirby.php';

// create an instance of the new `DbKirby` class
$kirby = new CustomKirby();

echo $kirby->render();

Then create the custom App class in the given location and add a custom site class.

1 Like

Thank you very much … i’ll try that out quickly and let you know :slight_smile:

In your custom Kirby class, you will then have to use your custom site model in these two methods from App.php:

    /**
     * Sets a custom Site object
     *
     * @param \Kirby\Cms\Site|array|null $site
     * @return $this
     */
    protected function setSite($site = null)
    {
        if (is_array($site) === true) {
            $site = new Site($site + [ // instantiate new custom site model here
                'kirby' => $this
            ]);
        }

        $this->site = $site;
        return $this;
    }
   /**
     * Initializes and returns the Site object
     *
     * @return \Kirby\Cms\Site
     */
    public function site()
    {
        return $this->site = $this->site ?? new Site([
            'errorPageId' => $this->options['error'] ?? 'error',
            'homePageId'  => $this->options['home']  ?? 'home',
            'kirby'       => $this,
            'url'         => $this->url('index'),
        ]);
    }
1 Like

Thank you once again.

What I want to achieve is virtual pages (from a database) like described here, but starting from the very first level (site level, not a page under the site).

Am I on the right track with this?

I have never tested this yet, but that’s what I would (try to) do as well. It’s basically the same approach that I used in this (unpublished) recipe about users from a database: Add cookbook recipe virtual users from database by texnixe · Pull Request #1421 · getkirby/getkirby.com · GitHub

It worked creating the custom App so far.

But how to overwrite the children() method in the new Site now? :thinking:

Similar to how it is done in the Virtual pages guide for the children of a given parent. But keep in mind that the site usually has other children like the home or error pages (and probably others), so you would have to make sure that you merge the parent class children with your virtual children.

Just defining a children() method doesn’t work. I ended up adding a custom method like
this

'children' => $this->myChildren()

in the new Site() call, defining an own children method. That works, but when i try to create a list of Pages using return Pages::factory($mypages, $this); it gives me an error (becase $this is not a Page model in that moment).

I made a little test to check this out myself and created this children method in my custom site model:

<?php

use Kirby\Cms\Site;
use Kirby\Cms\Pages;

class CustomSite extends Site
{

  public function children() {
    $children = parent::children();
    $pages= [
      [
        'slug'     => 'page-1',
        'template' => 'default',
        'model'    => 'default',
        'content'  => [
            'title'    => 'Page 1',
        ],
      ],
      [
        'slug'     => 'page-2',
        'template' => 'default',
        'model'    => 'default',
        'content'  => [
            'title'    => 'Page 2',
        ],
      ],
  ];
    $virtualChildren = Pages::factory($pages, $this);
    return $children->merge($virtualChildren);
  }
}

I don’t really understand why you had this error with $this, it doesn’t have to be a page instance.

1 Like

Thank you so much for your support. I mixed up the Classes and got that error, but now everything works perfect with the content from a database.

Funny. Just last week i was strugeling with that case too. At the same day @texnixe wrote a cookbook for that. Thank you. :raised_hands: