Code Typing
This template tries to make Kirby development more accessible by adding PHP code typing and auto completion. Sadly, this doesn't work straight out of the box.
Controllers
For controllers & other PHP files, we can add type declarations by importing the classes using PHP’s use
:
<?php // site/controllers/article.php
use Kirby\Cms\App;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
return function (Site $site, App $kirby, Page $page) {
[…]
}
Templates
Templates will receive variables defined by Kirby (like the $page
and $kirby
objects), and any other variable you return in a controller. Unfortunately, we can't declare them in PHP directly, so we need to use the PHPDoc @var tag.
<?php // site/templates/article.php
/** @var Kirby\Cms\Page $page */
<h1><?= $page->title() ?></h1>
As PHPDoc comments aren't a native PHP feature, this won't affect how our code runs, although all IDEs and most code editors (like VS Code) should support them.
Page models
If we're using a Page model to expand Kirby's default page object, we can use it in our templates in the same way.
<?php // site/models/article.php
class ArticlePage extends Kirby\Cms\Page {
public function getArticleBody(): string {
if ($this->content()->body()->isNotEmpty()) {
return $this->content()->body()->markdown();
}
return '';
}
}
<?php // site/templates/article.php
/** @var \ArticlePage $page */
<h1><?= $page->title() ?></h1>
<?= $page->getArticleBody() ?>
For classes reference, check out the Kirby reference.
Auto completion in VS Code
For excellent PHP support in VS Code, we use PHP Intelephense. Follow the Quick Start instructions. Other IDEs like PhpStorm may support this out-of-the-box.