Help using routes on subpage

Hi all !

Hope my explanation is clear enough :

I’m trying to understand this example where it explain how to remove the blog folder name form the URL, achieving something like this :

site/blog/article-type-a
into
site/article-type-a

site/blog/article-type-b
into
site/article-type-b
…etc

It works as it should, but now I don’t understand how to add another rule and do the same for URL of subpages inside the ‘article-types’.

site/blog/article-type-a/article-1
into
site/article-type-a/article-1

Thanks in advance for any cue.

Théo G.

1 Like

Try using (:all) instead of (:any)?

(:any) stops when it encounters a slash in the URL.

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

      $page = page($uid);

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

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

    }
  ),
  array(
    'pattern' => 'blog/(:all)',
    'action'  => function($uid) {
      go($uid);
    }
  )
));

Problem is that the (:all) placeholder currently does not work. You can either use (.+) instead, or use (:any)/(:any).

1 Like

Hi
Thanks guys for the help =).

I tried both propositions without success.

Using (:any)/(:any) there’s some redirection but not as expected.

site/blog/article-type-a
stays as
site/blog/article-type-a

But redirects to the “blog” page.

When the URL is : site/blog/article-type/article it goes to the article but “blog” stays in the URL.

Hi sorry to come back on that same question,
I still don’t get how to work with routes.

Using the suggested route, the site/blog/article-type-a URL stays the same but return the “/blog” page instead of the "/article-type"
What i’m trying to achieve is remove “blog” from “site/blog/article-type/article”

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

      $page = page($uid);

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

      return site()->visit($page);
    }
  ),
  array(
    'pattern' => 'blog/(:any)/(:any)',
    'action'  => function($uid) {
      go($uid);
    }
  )
));

Maybe someone knows of a comprehensive guide to learn about routing in php.

But this should work:

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

      $page = page($uid);

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

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

    }
  ),
  array(
    'pattern' => 'blog/(.+)',
    'action'  => function($uid) {
      go($uid);
    }
  )
));

Using (.+) there’s no change to the ‘site/blog/article-type-a’ url.
But when going to the ‘home’ page now it returns the ‘blog’ page instead.

Hi,

Found a solution digging into the forum.

Dont know if its the proper way of doing it but seems to work :

c::set('routes', array(
   array(
      'pattern' => 'clients/(:all)',
      'action'  => function($uid) {
         go($uid);
      }
   ),
   array(
      'pattern' => '(:any)(:all)',
      'action'  => function($u1,$u2) {
         $page = page($u1.$u2);

         if(!$page) $page = page('clients/' . $u1.$u2);
         if(!$page) $page = site()->errorPage();

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

This is the the original reply

2 Likes