MageWork

Back to home

Add a new package

Configuration

Open the configuration file for your current environment: etc/config.{MW_ENVIRONMENT}.php

<?php

$config = [
    'packages' => [
        'www.example.com' => [ // HTTP Host
            'Example' => '', // Package name (directory) and URL prefix (empty = no prefix)
            'Admin' => 'manager', // "/manager" URL path will load the "Admin" package
        ],
        'blog.example.com' => [ // HTTP Host
            'Blog' => '', // Package name (directory) and URL prefix (empty = no prefix)
        ],
    ],
    /* ... */
];

In this example, we use 3 packages in the same application:

By convention, the package name must start with a capital letter.

If you are using a port other than 80 or 443, the port must be included in the host, e.g. : "127.0.0.1:8080".

If you have defined local for the MW_ENVIRONMENT variable, open: etc/config.local.php.

Add the host for your local, the package name, and the URL prefix:

<?php

$config = [
    'packages' => [
        'localhost.acme' => [ // HTTP Host
            'Acme' => '', // Package name (directory) and URL prefix (empty = no prefix)
        ],
    ],
    /* ... */
];

Perform the operation again for each of your environments, and change the host. For example: etc/config.prod.php

<?php

$config = [
    'packages' => [
        'www.example.com' => [
            'Acme' => '',
        ],
    ],
    /* ... */
];

Tree structure

Next, create the package directory structure:

packages > Acme > etc > pages.php

<?php

$config = [
    Core_Page::TYPE => [
        'default' => [ // Data shared for all pages
            'template' => 'page', // packages/Acme/template/page.phtml
            'language' => 'en',
        ],
        '404' => [ // Identifier loader when requested page is not found
            '_http_code' => 404,
            'content' => 'content/error', // packages/Acme/template/content/error.phtml
            'meta_title'  => 'Not Found',
            'meta_description' => 'This page doesn\'t exist',
        ],
        '/' => [ // Homepage
            'class' => Acme_Page_Index::class,
            'content' => 'content/index', // packages/Acme/template/content/index.phtml
            'meta_title' => 'Home',
            'meta_description' => 'Homepage',
        ],
    ],
];

packages > Acme > Page > Index.php

<?php

declare(strict_types=1);

class Acme_Page_Index extends Core_Page
{
    public function getFoo(): string
    {
        return 'Bar';
    }
}

The execute method is called before template rendering. It allows you to implement the code logic and inject data into the template.

packages > Acme > template > page.phtml

<!DOCTYPE html>
<html lang="<?= App::escapeHtmlAttr($this->getLanguage()) ?>">
    <head>
        <title><?= App::escapeHtml($this->getMetaTitle()) ?></title>
        <?php if ($this->getMetaDescription()): ?>
            <meta name="description" content="<?= App::escapeHtmlAttr($this->getMetaDescription()) ?>" />
        <?php endif; ?>
        <link rel="stylesheet" href="<?= $this->getAssetUrl('css/style.css') ?>" type="text/css" />
        <script type="text/javascript" src="<?= $this->getAssetUrl('js/app.js') ?>"></script>
    </head>
    <body>
        <img src="<?= $this->getAssetUrl('media/logo.png') ?>" alt="Acme" />
        <?= $this->include($this->getContent()) ?>
    </body>
</html>

To display the page content, we include the content template file path.

packages > Acme > template > content > index.phtml

<?= App::escapeHtml($this->getFoo()) ?>

Access to http://localhost.acme in your browser!

CLI page debug

You can display a page directly in the console, for debugging or CI/CD testing.

php pub/index.php {environment} {host} {path} {developer_mode}

Example:

php pub/index.php local localhost.acme /