Leafiny Documentation

Routes

Routes are defined with identifier key of page object in module configuration files.

New page

Open the Frontend module config file: /modules/Frontend/etc/config.php, and declare a new route as following:

/** modules/Frontend/etc/config.php **/

'page' => [
    /* ... */
    '/example.html' => [
        'title'            => 'Example Page',
        'content'          => 'Frontend::page/example.twig',
        'meta_title'       => 'Example',
        'meta_description' => 'This is an example page',
    ],
    /* ... */
],
/* ... */

A new page is created with example.html path (https://…/example.html). The route is a page with a twig template for content.

Execute code before render

We update the previous route for execute any PHP code before render the page:

/** modules/Frontend/etc/config.php **/

'page' => [
    /* ... */
    '/example.html' => [
        'title'            => 'Example Page',
        'content'          => 'Frontend::page/example.twig',
        'meta_title'       => 'Example',
        'meta_description' => 'This is an example page',
        'class'            => Frontend_Page_Example::class
    ],
    /* ... */
],
/* ... */

A class key is defined with the name of the class. Add the new class in /modules/Frontend/app/Frontend/Page directory:

<?php
/** modules/Frontend/app/Frontend/Page/Example.php **/

declare(strict_types=1);

/**
 * Class Frontend_Page_Example
 */
class Frontend_Page_Example extends Core_Page
{
    /**
     * Execute action
     *
     * @return void
     */
    public function action(): void
    {
        parent::action();

        $this->setCustom('my_var', 'Hello World!');
    }
}

The name of the class must respect the name of the folder in module app directory: Frontend_Page_Example = Frontend/Page/Example.php

The action method is always called before render a page. Here we add a new custom variable to show in the page template:

<!-- modules/Frontend/template/page/example.twig -->

{{ page.getCustom('my_var') }} <!-- Hello World! -->

Execute code and redirect

A route can be used for execute code without a page render, like a form POST analyze:

/** modules/Frontend/etc/config.php **/

'page' => [
    /* ... */
    '/example/post/' => [
        'template' => null,
        'class'    => Frontend_Page_Form_Post::class
    ],
    /* ... */
],
/* ... */

With null value for the template key, the route show a blank page.

Add the class in /modules/Frontend/app/Frontend/Page/Form directory:

<?php
/** modules/Frontend/app/Frontend/Page/Form/Post.php **/

declare(strict_types=1);

/**
 * Class Frontend_Page_Form_Post
 */
class Frontend_Page_Form_Post extends Core_Page
{
    /**
     * Execute action
     *
     * @return void
     */
    public function action(): void
    {
        parent::action();

        $form = $this->getPost();

        /** Code logic **/

        $this->redirect($this->getUrl());
    }
}

The route redirect to home page after the custom code logic.

The name of the class must respect the name of the folder in module app directory: Frontend_Page_Form_Post = Frontend/Page/Form/Post.php