PHPackages                             ntriga/pimcore-link-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ntriga/pimcore-link-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ntriga/pimcore-link-generator
=============================

This is my package pimcore-link-generator

11.0.7(3mo ago)2184MITPHPPHP ^8.1

Since Sep 20Pushed 3mo agoCompare

[ Source](https://github.com/ntriga/pimcore-link-generator)[ Packagist](https://packagist.org/packages/ntriga/pimcore-link-generator)[ Docs](https://github.com/ntriga/pimcore-link-generator)[ GitHub Sponsors](https://github.com/ntriga)[ RSS](/packages/ntriga-pimcore-link-generator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (9)Used By (0)

Easy link generator for Objects
===============================

[](#easy-link-generator-for-objects)

[![Latest Version on Packagist](https://camo.githubusercontent.com/13725c7f9030d141d77be4a4d60d20a5468bb1e409e0880ca13896d5a04c3e35/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e74726967612f70696d636f72652d6c696e6b2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ntriga/pimcore-link-generator)[![Total Downloads](https://camo.githubusercontent.com/a2415a31a205bfb3ff455c319a7e1f3291822b6823993f54ec2f1271d6365615/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6e74726967612f70696d636f72652d6c696e6b2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/ntriga/pimcore-link-generator)

Generate urls for objects using the NtrigaLinkGenerator.
The links will have this format:

```
.../_locale/{document_path}/{unique_id_letter}{objectId}/{objectSlug}

```

Reasoning Behind URL Format
---------------------------

[](#reasoning-behind-url-format)

- 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.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require ntriga/pimcore-link-generator
```

Usage
-----

[](#usage)

Follow these steps to use the link generator.

### Create property on the home document

[](#create-property-on-the-home-document)

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.

E.g. "news\_document" [![document settings.png](https://github.com/ntriga/pimcore-link-generator/raw/main/images/document%20settings.png?raw=true)](https://github.com/ntriga/pimcore-link-generator/blob/main/images/document%20settings.png?raw=true)

### Create an action with the correct annotation

[](#create-an-action-with-the-correct-annotation)

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+"})
*/
```

### Create LinkGenerator class for the object

[](#create-linkgenerator-class-for-the-object)

```
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';
    }
}
```

### Register the LinkGenerator as a service

[](#register-the-linkgenerator-as-a-service)

Add the LinkGenerator as a service in your services.yaml file.

```
services:
    Ntriga\FrontBundle\LinkGenerator\NewsLinkGenerator:
        public: true
```

### Create a twig extension

[](#create-a-twig-extension)

Create a twig extension that will be used to generate the link.

```
