ntriga/pimcore-link-generator
Composer 安装命令:
composer require ntriga/pimcore-link-generator
包简介
This is my package pimcore-link-generator
README 文档
README
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.
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.
ntriga/pimcore-link-generator 适用场景与选型建议
ntriga/pimcore-link-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 195 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「ntriga」 「pimcore-link-generator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ntriga/pimcore-link-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ntriga/pimcore-link-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ntriga/pimcore-link-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Cookie banner for ntriga projects
Optimize the database tables in messages
Generates thumbnails of assets so they don't need to be generated on the fly
Package to streamline vue translations with Pimcore
Put Pimcore in the Ntriga style
Use Deepl in your Pimcore project to translate documents and objects.
统计信息
- 总下载量: 195
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-20
