Slug based on content field instead of folder name?

Hey,

Does anyone know if it’s possible to make a page’s slug to be based on a content field field instead of the folder name (default behaviour)?

For example: let’s say I have this structure: content/1-articles/1/ and inside that folder I got article.txt which has “Slug: the-first-article” so instead of getting “domain.com/articles/1/” url I would like to get “domain.com/articles/the-first-article/”.

Is this doable somehow?

This can be done with the router. Untested of the top of my head:

c::set('routes', array(
  array(
    'pattern' => 'articles/(:any)',
    'action'  => function($slug) {
        $site = site();
        $articles = page('articles');
        if ($article = $articles->children()->visible()->findBy('slug', $slug)) {
            return $site->visit($article);
        } else {
            return $site->visit('error');
        }
     }
  )
));

More info on the router is in the docs.