Screencast #02 - Extending Pagekit using Modules

Extending Pagekit using Modules

As a developer, you can easily extend what Pagekit already offers. Whether you want a custom theme or an extension for additional functionality, both are built following the same approach. In this post and the accompanying video, we will introduce what a package and what a module is - both central concepts of Pagekit.

Terminology: Packages and modules

Themes and extension might seem different from the outside, but they are both something we call a package. A package is just a folder that bundles a number of files and contains meta data in a file called composer.json.

To register anything with the Pagekit system, you also include a file called index.php, which contains the definition for something we call a module. A module is the most important building unit in the Pagekit code. Modules are self contained units that bundle functionality and are combined to form the whole system. Modules are used both in the Pagekit core and in any third party extensions.

Where to put your package files

Every package belongs to a specific vendor name, which becomes clear when you look at the directory structure of a fresh Pagekit installation.

Core packages are part of the pagekit namespace and located in the appropriate subdirectory inside /packages. Any third party packages (yours!) will have their own vendor name and therefore sit in a separate subfolder.

/packages
    /pagekit
        /blog
        /theme-one
    /your-vendor
        /your-theme
        /your-extension

In every package you will find at least 2 files: composer.json and index.php.

1. composer.json: The package meta data

The composer.json contains meta data for the package. This is used to display in the Pagekit backend and if you upload your extension to the Pagekit marketplace.

packages/pagekit/todo/composer.json

{
    "name": "todo",
    "version": "0.1",
    "type": "pagekit-extension",
    "title": "ToDo management"
}

2. index.php: The module definition

Modules bundle functionality inside Pagekit and are the way for you to hook into the Pagekit system. From a code perspective, the module definition is a PHP array that has certain properties set. The index.php always has to return a valid array.

<?php

use Pagekit\Application;

// packages/pagekit/todo/index.php

return [

    'name' => 'todo',
    
    'type' => 'extension',

// called when Pagekit initializes the module
    'main' => function (Application $app) {
        echo "It's alive";
    }
];

?>

In order to test the functionality, make sure you enable the extension in the backend. When it’s active you will see the printed output at the top of your screen.

This minimal example shows how small a fully functional module can be. It has access to the Application instance. With this object you can access all services, trigger events and listen to events triggered by other modules.

This was straight forward and already gives us much power. However, there are many more properties available for the module definition. We will look at some of them in the next part of our posts. If you do not want to wait till then, check out the available module properties in the Pagekit documentation.

Here is a short list of all existing screencasts:

Florian
Posted by Florian
{{ message }}

{{ 'Comments are closed.' | trans }}