Generate urls for objects using the NtrigaLinkGenerator.
The links will have this format:
.../_locale/{document_path}/{unique_id_letter}{objectId}/{objectSlug}
- Based on the ID, we can load the page a bit faster, which is important for UX and SEO.
- The length of the path has no importance for SEO, and the path is only used as an index. For this, we provide canonical URLs.
- In the SERP, the path is no longer displayed; instead, breadcrumbs are shown.
- The title/slug appears at the end of the path instead of the ID, making it clearer for the user.
You can install the package via composer:
composer require ntriga/pimcore-link-generator
Follow these steps to use the link generator.
Create a property on the home document of the type "document" that is inheritable.
This property will store the parent page of the object.
This makes it easy to link another document for each language.
Create an action that will be used to generate the link.
The annotation should be this format
/**
* @Route("{path}/n{objectId}/{objectSlug}", name="news-detail", defaults={"path"=""}, requirements={"path"=".*?", "objectSlug"="[\w-]+", "objectId"="\d+"})
*/
namespace Ntriga\FrontBundle\LinkGenerator;
use Ntriga\PimcoreLinkGenerator\NtrigaLinkGenerator;
use Pimcore\Model\DataObject\News;
class NewsLinkGenerator extends NtrigaLinkGenerator
{
/*
* Here you can set the name of the property that is used to store the parent document
*/
protected function getDefaultDocumentPropertyName(): string
{
return 'news_document';
}
/*
* Here you can set the class name of the object
*/
protected function getObjectClassName(): string
{
return News::class;
}
/*
* Here you can set the route name of the action that will be used to generate the link
*/
protected function getRouteName(): string
{
return 'news-detail';
}
/*
* Optional
* Here you can set the name of the method that will be used to generate the slug
* Default is "getName"
* If you have a slug input field, this will be used instead (not a field of the type slug)
*/
protected function getObjectDefaultSlugField(): string
{
return 'getTitle';
}
}
Add the LinkGenerator as a service in your services.yaml file.
services:
Ntriga\FrontBundle\LinkGenerator\NewsLinkGenerator:
public: true
Create a twig extension that will be used to generate the link.
<?php
namespace Ntriga\FrontBundle\Twig\Extension;
use Ntriga\FrontBundle\LinkGenerator\NewsLinkGenerator;
use Pimcore\Model\DataObject\News;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class NewsExtension extends AbstractExtension
{
public function __construct(
protected NewsLinkGenerator $newsLinkGenerator
) {}
public function getFunctions()
{
return [
new TwigFunction('app_news_detaillink', [$this, 'generateLink']),
];
}
public function generateLink(News $item): string
{
return $this->newsLinkGenerator->generate($item, []);
}
}
Add the twig extension as a service in your services.yaml file.
services:
Ntriga\FrontBundle\Twig\Extension\NewsExtension:
tags: ['twig.extension']
<a href="{{ app_news_detaillink(newsItem) }}">News detail</a>
With this strategy, the detail page will be accessible via multiple URLs. So it's important you set a canonical link on the detail page.
Please see CHANGELOG for more information on what has changed recently.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.