How does routing work?

Hello,

I’m new to Kirby, but I’ve used WordPress, Expression Engine, Habari and other CM-Systems since 2004.

What is new to me, is the aspect of routing. I’ve read the docs and nearly all topics about this in the forum, but I think I’m totally missing something.

In the docs you have the example of a WordPress-like structure and this is going with c::set into config.php. But that’s not all right? Somewhere I’ve read that I must start und run a Router, but how exactly should I do this and in which subfolder? Could you please give a hint to a complete workflow for changing the urls of blog articles from

kirby.com/blog/my-article to
kirby.com/blog/2018/04/my-article

I’m used to PHP but I’m not a specialist in OOP.

You don’t have to start a router to start using routing. You can put the code as described in the routing docs here into your config.php file. So the code for omitting the blog folder while using Wordpress like URLS: https://getkirby.com/docs/developer-guide/advanced/routing#simulating-wordpress-urls

c::set('routes', array(
  array(
    'pattern' => 'blog/(:num)/(:num)/(:num)/(:any)',
    'action'  => function($year, $month, $day, $uid) {

      // search for the article
      $page = page('blog/' . $uid);

      // redirect to the article or the error page
      go($page ? $page->url() : 'error');

    }
  )
));

If you don’t want to use the day, leave out one of the (:num) placeholders in the pattern

'pattern' => 'blog/(:num)/(:num)/(:any)',
'action'  => function($year, $month,  $uid) {

Edit: I think I’d change the code from the docs a bit:

c::set('routes', array(
  array(
    'pattern' => 'blog/(:num)/(:num)/(:any)',
    'action'  => function($year, $month, $uid) {

      // search for the article
      $page = page('blog/' . $uid);

      if(!$page) $page = site()->errorPage();
      // redirect to the article or the error page
      return site()->visit($page);

    }
  )
));

Thank you for the fast reply texnixe.

Yes I’ve read that and put exactly this in the config.php. Must I then link to that url explicit or should the article-links get that type of url automaticly? Either way, if I enter the url directly I get an error page and otherwise there are no changes to the urls, in the panel for instance.

Maybe I’ve changed some things in the last days testing Kirby… I could do a fresh install…

Okay, with your edited example it works. I can call the new url! :slight_smile: I have a little fear about double-content then, cause I can adress an article with to urls?

Kirby doesn’t create those links automatically. But you can use a page model that does this for you, for example:

<?php
class ArticlePage extends Page {
  public function url() {
    return url('blog/'. $this->date('Y/m').'/'.$this->uid());
  }
}

With this model, you overwrite the native url()method for all article pages, i.e. all pages that use the article.php template and thus the article.php model.

Well, with the above model in place, that shouldn’t be an issue, because the original URL wouldn’t be known to search engines because it isn’t used anywhere. But I’m no SEO expert, whatever that is.

Thanks a lot for your instant help! :slight_smile: