Omiting folder in the URL for all subpages


#1

I’m trying to omit the folder ‘news’ in the URL for all its subpages.

The next code works fine for the first level pages but I don’t know how to add a second level.

`
c::set('routes', array(

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

     $page = page($uid);

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

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

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

Any help?


#2

This should work if I got you right, replace blog with news:

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

#3

Yes it works, but changing the order of one array:

c::set('routes', array(
  
  array(
    'pattern' => '(: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)',
    'action'  => function($uid) {
      go($uid);
    }
  ),
  array(
    'pattern' => '(:any)/(:any)',
    'action'  => function($parent, $uid) {
      $page = page($parent . '/' , $uid);
      
      if(!$page) $page = page('blog/' . $parent . '/' . $uid);
      if(!$page) $page = site()->errorPage();
      return site()->visit($page);
    }
  ), 
 array(
    'pattern' => 'blog/(:any)/(:any)',
    'action'  => function($parent, $child) {
      go($parent .'/' . $child);
    }
    )
 ));

I also have found this alternative in this forum:

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

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

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

It has a few lines less but I don’t know if is better or not.

Thanks @texnixe