MageWork

Back to home

Add a new package

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:

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' => '',
        ],
    ],
    /* ... */
]

Next, create the package directory structure:

packages > acme > etc > pages.php

<?php

$config = [
    'acme' => [
        Core_Page::TYPE => [
            'default' => [ // Data shared for all pages
                'template' => 'page', // packages/acme/template/page.phtml
                'language' => 'en',
            ],
            '404' => [ // Identifier loader when no page is found
                'content' => 'content/error', // packages/acme/template/content/error.phtml
                'title'  => 'Not Found',
                'description' => 'This page doesn\'t exist',
            ],
            '/' => [ // Homepage
                'class' => Magework_Page_Index::class,
                'content' => 'content/index', // packages/acme/template/content/index.phtml
                'title' => 'Home',
                'description' => 'Homepage',
            ],
        ],
    ],
];

packages > acme > app > Acme > Page > Index.php

<?php

declare(strict_types=1);

class Acme_Page_Index extends Core_Page
{
    public function execute(): void
    {
        $this->setFoo('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->getTitle()) ?></title>
        <meta name="description" content="<?= App::escapeHtmlAttr($this->getDescription()) ?>" />
        <link rel="stylesheet" href="<?= $this->getSkinUrl('css/style.css') ?>" type="text/css" />
        <script type="text/javascript" src="<?= $this->getSkinUrl('js/app.js') ?>"></script>
    </head>
    <body>
        <img src="<?= $this->getMediaUrl('logo.png') ?>" alt="Acme" />
        <?= $this->getBlock($this->getContent()) ?>
    </body>
</html>

packages > acme > template > content > index.phtml

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

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

To display the page content, we call a block with the content template file path.