Unfortunately I can’t get the page model to work. I don’t get what I’m doing wrong here. Any ideas?
/site/models/contact.php
<?php
class ContactPage extends Page
{
public function foo(): string
{
return 'Foo';
}
}
/site/templates/contact.php
<?php var_dump($page->foo()); ?>
Result
object(Kirby\Content\Field)#387 (1) { ["foo"]=> NULL }
Kirby 4.1.2
Make sure that contact page have contact.txt
content file: /content/contact-us/contact.txt
Thanks for the fast response @ahmetbora. Yes, that should be ok already - the filename is contact.de.txt
.
Just tested the output of $page->intendedTemplate()->name()
on the page and it returns contact
.
Maybe there is more to consider?
So your setup is correct and should be work fine. I’ve tested on 4.1.2 and works as expected. Do you have any plugins? Seems some setup broke the calling models.
You were right, the problem is caused by a plugin. Inside the route function it used $kirby->page();
. This caused the wrong page model to be loaded. At the moment I’m not sure how to workaround this.
'routes' => function ($kirby) {
...
$kirby->page('pageid');
...
Hmm, could you share which plugin? Maybe I can check or help with fix it.
It is not public. In general it is a lightweight plugin for managing redirects. Maybe you have a fast idea for a rewrite?
'routes' => function ($kirby) {
$routes = [];
if ($kirby->page('redirects')) {
foreach($kirby->page('redirects')->redirects()->toStructure() as $redirect) {
$routes[] = [
'pattern' => $redirect->from()->toString(),
'action' => function () use ($redirect) {
go($redirect->to()->toUrl(), $redirect->statusCode()->toInt(301));
}
];
}
}
return $routes;
}
I found fast workaround, not elegant but maybe ok
$kirbyClone = clone $kirby;
then use $kirbyClone
instead of $kirby