Remove parent page name in subpage url


#1

Hi everyone,

I want to remove in a url of a subpage the name of the its parent page. I’ll give an example:

I have this:
page.com/news/category/new

I want this:
page.com/news/new

How can I do this? I’ve tried with routes but I can’t .

Thank you :slight_smile:


#2

You have to do two things:

  1. If you want to link to these pages, you have to change the url()method for these pages. using. a. Page Model
  2. You have to implement a route similar to Omitting the blog folder in URLs, only one level. deeper down.

This. should. work:

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

      $page = page('news/'.$uid);

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

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

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

#3

Hi, thanks. But my problem is that /category is not the name of the page, it has a custom name. for example:

news/my-custom-category/my-new


#4

How many categories are there and are they dynamic or fixed?


#5

They are dinamic. Now I have four but the client can create any number of them.


#6

And what about the category pages themselves, should they still be accessible, because they display their corresponding subpages, or are they just containers?


#7

They are only container to organize the news.


#8

Don’t know if. this is the best way to go about this, but try:

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

      if($page = page('blog/'.$uid)) {
        go($page->parent());
      } else {

        $categories = page('blog')->children()->pluck('uri', ',');
        $i = 0;

        while($i < count($categories)) {
          if($page = page($categories[$i].'/'.$uid)) {
            return site()->visit($page);
          }
             $i++;
        }
      }


          return false;




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

BTW. When a topic is solved, could you please either let u. know and/or mark it as solved? There are quite a few of your questions here where I’m not sure if I can close them or not.

Edit: In the above, you have to replace blog of course…


#9

Ah sorry, I will close them. This I didn’t resolve yet. Thanks and sorry


#10

@jciaurriz continuing from @texnixe first solution, I recommend trying this:

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

      if(!$page) $page = page('news')->grandChildren()->findBy('uid', $uid);
      if(!$page) $page = site()->errorPage();

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

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

By using grandChildren it will look for pages 2 levels down from news. The first level could be category or anything else.


#11

Oh my, what can I say?