🚀 A new era: Kirby 4 Get to know
Skip to content

Simple virtual page

Add an ad-hoc virtual page quickly via a simple route

Using Kirby's router, you can quickly create a new page from a route pattern:

/site/config/config.php
<?php

use Kirby\Uuid\Uuid;

return [
    'routes' => [
        [
          'pattern' => 'virtual',
          'action'  => function () {
                return Page::factory([
                    'slug' => 'virtual',
                    'template' => 'virtual',
                    'model' => 'virtual',
                    'content' => [
                        'title' => 'This is not a real page',
                        'date'  => '2019-05-01',
                        'text'  => 'The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen.',
                        'uuid'  => Uuid::generate(),
                    ]
                ]);
          }
        ]
    ]
];

Unless you have disabled UUIDs in your config, you have to pass a uuid field in the content array to prevent Kirby from generating the page in the file system when the $page->uuid() method is called.

If you generate the UUIDs automatically like in the example above, they will change at every load. However, if you want to reference your virtual pages anywhere with their UUID, make sure to use a unique string that does not change.

Create a virtual.php template to output the content of the virtual page:

/site/templates/virtual.php
<?php snippet('header') ?>
<main>
  <article class="note">
    <header class="note-header intro">
      <h1><?= $page->title() ?></h1>
      <time class="note-date"><?= $page->date()->toDate('d F Y') ?></time>
    </header>

    <div class="note-text text">
      <?= $page->text()->kt() ?>
    </div>
  </article>
</main>
<?php snippet('footer') ?>

If you now open http://yourdomain.com/virtual in the browser, you will see the new page.

It's also possible to create a multi-language version of this page, by adding the translations array with the content for every language version.

/site/config/config.php
<?php

use Kirby\Uuid\Uuid;

return [
    'languages' => true,
    'routes' => [
        [
          'pattern' => '(de|en)/virtual',
          'action'  => function ($lang) {
                $data = [
                    'slug' => 'virtual',
                    'parent' => page('notes'),
                    'template' => 'default',
                    'num' => '20190501',
                    'translations' => [
                        'en' => [
                            'code' => 'en',
                            'content' => [
                                'title' => 'This is not a real page',
                                'date'  => '2019-05-01',
                                'text'  => 'The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen.',
                                'uuid'  => Uuid::generate(),
                            ]
                        ],
                        'de' => [
                            'code' => 'de',
                            'content' => [
                                'title' => 'Das ist keine Seite',
                                'date'  => '2019-05-01',
                                'text'  => 'Weit hinten, hinter den Wortbergen, fern der Länder Vokalien und Konsonantien leben die Blindtexte. Abgeschieden wohnen sie in Buchstabhausen an der Küste des Semantik, eines großen Sprachozeans.'
                            ]
                        ]
                    ],
                ];

                $page = Page::factory($data);

                site()->visit($page, $lang);

                return $page;
          }
        ]
    ]
];

These hard coded examples are not that useful yet, but they should already give you an idea how the factory method helps us to create pages from whatever content we pass to it.