Custom Routes: Disable parent Category

routes

#1

Hi guys,

I’m a new kirby user and it works amazing and everything is so well documented, thanks for creating such a great and simple cms.

Here is the problem I’m having currently … I’d like to kill the parent category in the url to make it more flat and simplify it. Basically I’d like to remove the parent category page.

This is how it usually is:
domain.tld/projects/project-a

This is how I’d like it to be:
domain.tld/project-a

I tried this snippet for the config file but it doesn’t work…

c::set('routes', array(
 array(
   'pattern' => array('(:any)'),
   'action'  => function($uid) {

     $page = page($uid);
     

     if(!$page) $page = page('projects/' . $uid);
     // if(!$page) $page = site()->errorPage();

     return site()->visit($page);

   }
 )
));

Any thoughts would be appreciated!

cheers,
Stefan


#2

Check out this example: https://getkirby.com/docs/developer-guide/advanced/routing#omitting-the-blog-folder-in-urls


#3

Amazing! Thanks for pointing to the right direction — works great!


#4

Hi everyone,

I have another problem that I can’t seem to figure out. The below code works great, but I added another layer on top which breaks then the route if you navigate to the lowest level.

it should all output to …
/projects
/project-a
/project-b

and not …

/projects/project-a
/projects/project-b

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);
    }
  )
));

I hope it’s clear. Basically all I want to achieve is to make the url as flat as possible by removing all parent and grandparent categories.

best,
Stefan


#5

But what is your structure now, currently you are only handling 2 levels, parent and child.


#6

Good Point :wink:

that’s the deepest route …
domain.com/projects/category/project-name


#7

Check out the example here: Inner pages with custom URLs


#8

Ah great! That would work perfectly, could we do this also dynamically? So you don’t have to hardcode the specific route?

I’m trying a few things but I’m not sure if this https://getkirby.com/docs/cheatsheet/request/path resolves it?

Thanks for your help!


#9

The request path is not so useful here, because it only consists of this one variable we get from the placeholder.

What you need. instead is an array of possible pages you can loop through, rather than typing them all out.

For example, get the. index of the projects folder:

$uris = page('projects')->index()->pluck('uri');

This gives you a list of page URIs you can loop through to compare with the page in. the pattern.

If you have a lot of categories that might be worth it.

Keep in mind that you really have to be careful with this URL flattening approach and make sure that none. of the subpages in any of your category folders have the same. UID.