定制 ntriga/pimcore-link-generator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ntriga/pimcore-link-generator

最新稳定版本:11.0.6

Composer 安装命令:

composer require ntriga/pimcore-link-generator

包简介

This is my package pimcore-link-generator

README 文档

README

Latest Version on Packagist Total Downloads

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

  • 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

You can install the package via composer:

composer require ntriga/pimcore-link-generator

Usage

Follow these steps to use the link generator.

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

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

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

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 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, []);
    }
}

Register the twig extension as a service

Add the twig extension as a service in your services.yaml file.

services:
    Ntriga\FrontBundle\Twig\Extension\NewsExtension:
        tags: ['twig.extension']

Use the twig extension in your templates

<a href="{{ app_news_detaillink(newsItem) }}">News detail</a>

Make sure the detail page contains a canonical link

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.

Additional features

Query parameters

You can add query parameters to the generated URL by passing them in the queryParams array:

// Single parameter
$link = $productLinkGenerator->generate($object, [
    'queryParams' => ['color' => 'red']
]);
// Result: /en/products/my-product?color=red

// Multiple parameters
$link = $productLinkGenerator->generate($object, [
    'queryParams' => [
        'color' => 'red',
        'size' => 'large',
        'category' => 'shirts'
    ]
]);
// Result: /en/products/my-product?color=red&size=large&category=shirts

// Combined with other parameters
$link = $productLinkGenerator->generate($object, [
    'document' => $document,
    '_locale' => 'en',
    'queryParams' => ['color' => 'red']
]);
// Result: /en/products/my-product?color=red

Changelog

Please see CHANGELOG for more information on what has changed recently.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 168
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-20