I would like to modify the URL in my current project so that the parent page slug is omitted for certain pages. The project is also multilingual (DE, EN) and the URLs are partial multilevel.
I have used the following instructions and modified them accordingly:
and
Now I would like to remove the parent slugs from the XML sitemap as well.
I have already coded the following, but I am not sure if there is a better or more performant way to do this.
thank you for the quick reply.
I have now solved it using Page Model.
use Kirby\Cms\Page;
class CollectionLightPage extends Page {
public function url($options = null): string {
$langCode = kirby()->language()->code();
$removeFromPath = ['kollektionen','collections'];
$_removeFromPath = A::map($removeFromPath, function ($item) {
return sprintf('/%s/', $item);
});
return str_replace($_removeFromPath, '/', parent::url($langCode));
}
}
To remove really only the desired part of the path, and no other strings in the URL, I wrapped everything in “/” with the A::map() method before replacing, but I’m still not sure if this is the most elegant way to do it…
I am not familiar with custom page methods…
I’m also still struggling a bit with the appropriate routing …
Hi,
now I noticed that in the sitemap the urls where I overwrote the url() method (with the page model) don’t have any language-code. For all other pages it works fine. Here is the code for the sitemap.
I’ve been trying for almost the whole weekend now, but I can’t get it to work.
It seems that on pages which I customized with page model (see post above) the language-code is not set.
Also on pages where I have overridden the url method using page model the language code is apparently ignored. Here is an excerpt from my langunage navigation:
But in your method, you complete ignore a language code parameter and try to get the the language via kirby()->language()->code. This cannot work, because in the sitemap where you loop through many pages, this will return nothing useful. So when the option is passed, you have to use it in your method body.
You should also set $this->url when returning, see the original method.
'routes' => [
[
'pattern' => '(:any)',
'action' => function($uid) {
$page = page($uid);
if(!$page) $page = page('actualites/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
],
[
// redirection des urls avec /actualites/{permalink-article} vers /{permalink-article}
'pattern' => 'actualites/(:any)',
'action' => function($uid) {
go($uid);
}
],
// Route for English pages without /news/
[
'pattern' => 'en/(:any)',
'action' => function($uid) {
$page = page('en/' . $uid);
if(!$page) $page = page('en/news/' . $uid);
if(!$page) $page = site()->errorPage();
return site()->visit($page);
}
],
// Redirect English URLs with /news/ to the version without /news/
[
'pattern' => 'en/news/(:any)',
'action' => function($uid) {
go('en/' . $uid);
}
]
]
But it returns this error when I visit a blog post page
TypeError thrown with message "Kirby\Cms\Site::visit(): Argument #1 ($page) must be of type Kirby\Cms\Page|string, null given, called in /home/local/site/config/config.php on line 44"
Stacktrace:
#7 TypeError in /home/local/kirby/src/Cms/Site.php:476
#6 Kirby\Cms\Site:visit in /home/local/site/config/config.php:44
#5 Kirby\Http\Route:{closure} in [internal]:0
#4 Closure:call in /home/local/kirby/src/Http/Router.php:120
#3 Kirby\Http\Router:call in /home/local/kirby/src/Cms/App.php:338
#2 Kirby\Cms\App:call in /home/local/kirby/src/Cms/App.php:1191
#1 Kirby\Cms\App:render in /home/local/index.php:5
#0 require in /home/local/kirby/router.php:15
Should I do it the way @RoBoBo did it? But i’m on Kirby v4 now, what’s the best recommended way to manage this, any suggestion @texnixe?