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
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
You have to do two things:
url()
method for these pages. using. a. Page ModelThis. 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);
}
)
));
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
How many categories are there and are they dynamic or fixed?
They are dinamic. Now I have four but the client can create any number of them.
And what about the category pages themselves, should they still be accessible, because they display their corresponding subpages, or are they just containers?
They are only container to organize the news.
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…
Ah sorry, I will close them. This I didn’t resolve yet. Thanks and sorry
@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.
Oh my, what can I say?
Hi,
I tried this solution and it works but when the page is multilanguage I have a problem. I’ll show an example:
A new with this url (https://mysite.site/es/news/category/new) can change with the code you said me (https://mysite.site/es/news/new)
But when my page is setted to French https://mysite.site/fr/news/category/new it doesn’t run, 404 page appears. I think the problem is the name of the category that in french changes.
I wrote your code for each language that has my page and the only language that doesn’t run is french (the other ones are variations of Spanish and the names of the categories don’t change)