🚚 How to migrate to Kirby Tips & tutorials
Skip to content

Improve IDE support

Many code editors and IDEs offer support for code completion suggestions or showing meta information about your variables, method calls etc. To make the most use of it, your IDE needs to know what types your variables hold.

Templates and snippets

Kirby exposes some general variables like $page, $site etc. to your templates as well as anything returned in the page controller. Your IDE however does not automatically know what these variables stand for. You can change this by adding comments specifying their types:

site/templates/note.php
<?php
/**
 * @var Kirby\Cms\App $kirby
 * @var Kirby\Cms\Site $site
 * @var Kirby\Cms\Page $page
 */
?>

<ul>
  <?php foreach ($page->children() as $child): ?>
    <li><?= $child->title() ?></li>
  <?php endforeach ?>
</ul>

Your IDE can now infer that $child is also a Kirby\Cms\Page object.

Controllers

In your controllers, you can access the standard Kirby objects as arguments to your controller closure. By type-hinting them, your IDE can help you with completion suggestions etc.

site/controllers/note.php
<?php

use Kirby\Cms\App;
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Cms\Site;

return function (Site $site, Page $page, Pages $pages, App $kirby) {
    // ...
};

Page models

Make sure to do the same for your custom page models:

site/models/note.php
<?php

use Kirby\Cms\Page;

class NotePage extends Page
{
    // ...
}

Author