Routing 101 - Remove part of the URL

Hi again, forum! I’m trying but i can’t find info that works: lots of links to guides are broken and code bits don’t seem to work.

So, i don’t understand routing. What i want, i think, is the simplest example of routing, to hide the “projects” in “site.com/projects/work-project”.

So far i have this, but after LOTS of tries i got confused and now i don’t have a clue about what i’m doing (with my life):

return [
    'routes' => [[
        'pattern' => 'projects/(:any)',
        'action'  => function ($uid) {
            $page = page('projects/' . $uid);
            if($page && $page->intendedTemplate() == 'link') {
                return go($page->link(), 301);
           }
            return $page;
        }
    ]]
];

Edit: Also the “projects” page acts like a standalone page with its own contents. Does routing solve this too?

These are the old docs, but you basically need two routes (you will have to slightly adapt this example to your page names and to the new syntax but the principle is the same)

https://k2.getkirby.com/docs/developer-guide/advanced/routing#omitting-the-blog-folder-in-urls

Thanks again! I used that code bit before, got it from the forum, but it doesn’t work. I did replaced the “blog” instances… What could it be? I still don’t understand what this is doing… What is UID? Shouldn’t i put “projects/(:any)” in line 3?

c::set('routes', array(
    array(
        'pattern' => '(:any)',
        'action'  => function($uid) {
            $page = page($uid);
            if(!$page) $page = page('projects/' . $uid);
            if(!$page) $page = site()->errorPage();
            return site()->visit($page);
        }
    ),
    array(
        'pattern' => 'projects/(:any)',
        'action'  => function($uid) {
            go($uid);
        }
    )
));

As I said, if you use the new syntax in your config.php, it will/should work:

<?php

return [
    'debug' => false,
    'routes' => [
        [
            'pattern' => '(:any)',
            'action'  => function($uid) {
                $page = page($uid);
                if(!$page) $page = page('project/' . $uid);
                if(!$page) $page = site()->errorPage();
                return site()->visit($page);
            }
        ],
        [
            'pattern' => 'project/(:any)',
            'action'  => function($uid) {
                go($uid);
            }
        ]
    ]
];

2 Likes

Oh, i see. Sorry, i don’t understand the new syntax, can’t find documentation about it too. Is there any resource about it?

Still, it doesn’t work… Maybe it’s broken elsewhere? Any ideas on how to catch the problem? I’ve been testing with some random anchors scattered all over the DOM and it doesn’t ommit the “projects” part…

I tested the code in a Starterkit and it works. Have you tested this?

What you probably mean is that the links don’t change; you can take care of that with a Page model. In your Page model, overwrite the default url() method, so that it omits the parent folder.

Thanks for your help, i think i have to start hover. I was using the v2! The documentation changed recently and i thought my fresh download was version 3!

Do you know how to, or where i can find info about, convert to the new syntax? For example, in the following code, i don’t know how to convert the array:

c::set('languages', array(
    array(
        'code'    => 'en',
        'name'    => 'English',
        'default' => true,
        'locale'  => 'en_GB',
        'url'     => '/',
    ),
    array(
        'code'    => 'pt',
        'name'    => 'Português',
        'locale'  => 'pt_PT',
        'url'     => '/pt',
    ),
));

Check out the docs: https://getkirby.com/docs/guide/languages/introduction

That code now goes into separate language files in /site/languages.

Sorry for digging this up but I’m just testing this bit of code and ran into an issue. The route works fine at first glance but it seems to break the preview for Drafts. Previewing a Draft returns the error page instead of rendering the preview. Any ideas?

Check out these links:

Thank you Sonja - amazing support, as always!