Loading a (virtual) page model somewhere else in the code

I’m trying to load a page model that was created virtually in a plugin like this:

	'routes' => [
		[
			'pattern' => 'newsletter/subscriber',
			'method' => 'GET|POST',
			'action' => function () {
				$kirby = kirby();
				if (!($user = $kirby->user())) {
					go(url('/login'));
				}

				return new SubscriberPage([
					'slug' => 'newsletter/subscriber',
					'template' => 'subscriber',
					'model' => 'subscriber',
					'parent' => page('members'),
					'controller' => 'subscriber',
					'content' => [
						
					]
				]);
			}
		],
	],

The SubscriberPage extends Page and has some methods I want to access somewhere else.

Normally, I would do something like this: page('newsletter/subscriber')->myMethod() or page('members/newsletter/subscriber')->myMethod(), but that just returns null.

Is there another way to load myMethod() through the page class?

Thanks for any hints.

Slashes are not allowed in slugs.

But that’s not the main problem. A virtual page created in a route is not part of the site inventory and cannot be found with the page helper.

That page is created only as a response to a route, so it doesn’t exist elsewhere.

I think you might want to switch strategy and create a model for the members page, overwriting the children function, instead of the route and add your subscriber page there.

<?php 

class MembersPage extends Page {
    public function children() {
        $children = parent::children();
        $subscriber = new SubscriberPage([
            'slug' => 'subscriber',
            'template' => 'subscriber',
            'parent' => $this,
         // 'model' => 'subscriber', // not necessary here
         // 'controller' => 'subscriber', // controller = template
            'content' => [

            ]
        ]);
        return $children->add($subscriber);
    }
}

Thank you for the suggestions, @texnixe and @rasteiner; I will look into it further.
I apparently had this issue in the past :smiley: How to add virtual pages to kirby's index - #13 by marcus-at-localhost
I will report back.

Is it necessary to include a content file in /content/members/default.de.txt in order for the ‘MembersPage’ model to be recognized?

For the model to work, the content file needs to have the file name of template/model. So MembersPage only works for members.txt. A content file default.txt would need a model DefaultPage

1 Like