Skip to content

Kirby 3.5.7.1

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

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.'
                    ]
                ]);
          }
        ]
    ]
];

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

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.'
                            ]
                        ],
                        '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.