Routing

This page is about how to define routes with Kiwa.

Table of contents

Basic routes

In Kiwa, routes are defined in the main config file. This may look like that:

<?php

$config
    ->addPage(PageGenerator::create()
        ->setName('imprint')
        ->setLanguageCode('en')
        ->setChildOf('legal')
        ->setFile('imprint')
    )
;

Here we have a page named imprint with a language code en. The config says, that the page is a subpage of a page with the name legal.

Depending on the configuration of the URL structure, the example above will result in the route /en/legal/imprint.html.

The method setFile defines, that for this request a file named imprint.phtml will be loaded.

If you want to know more about how to create and handle links, head over to URLs and links.

Dynamic routes

It may happen that URLs with a dynamic part have to be defined. For example, in the case of a blog that has a main page /blog.html and an unspecified number of subpages, for example /blog/hello-world.html. These pages are defined together in Kiwa.

At first comes the definition of the main page:

<?php

$config
    ->addPage(PageGenerator::create()
        ->setName('blog')
        ->setDynamicChild('blog-dynamic-child')
        ->setFile('blog')
        // More configuration is needed here but not important for this example.
    )
;

Here the method setDynamicChild defines, that there are subpages allowed and should be routed into a page that is defined with the name blog-dynamic-child.

The example above will result in two route definitions. Depending on the configuration of the URL structure, the routes will be /blog.html and /blog/*.html.

Now we need the additional configuration of the subpage:

<?php

$config
    ->addPage(PageGenerator::create()
        ->setName('blog-dynamic-child')
        ->childOf('blog')
        ->setFile('blog-dynamic-child')
        // More configuration is needed here but not important for this example.
    )
;

The method childOf links the page to its parent and helps to create proper URLs.

For this request a file named blog-dynamic-child.phtml will be loaded.