abenmada / sylius-broken-link-handler-plugin
The Broken Link Handler plugin automatically redirects outdated product and taxon slugs to their new versions, ensuring they remain permanent and preventing their reuse by other entities
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 0
Open Issues: 0
Type:sylius-plugin
Requires
- php: ^8.0
- sylius/sylius: ^1.12
Requires (Dev)
- behat/behat: ^3.6.1
- behat/mink-selenium2-driver: ^1.4
- dmore/behat-chrome-extension: ^1.3
- dmore/chrome-mink-driver: ^2.7
- friends-of-behat/mink: ^1.8
- friends-of-behat/mink-browserkit-driver: ^1.4
- friends-of-behat/mink-debug-extension: ^2.0.0
- friends-of-behat/mink-extension: ^2.4
- friends-of-behat/page-object-extension: ^0.3
- friends-of-behat/suite-settings-extension: ^1.0
- friends-of-behat/symfony-extension: ^2.1
- friends-of-behat/variadic-extension: ^1.3
- friendsofphp/php-cs-fixer: ^3.9
- phpspec/phpspec: ^7.2
- phpstan/extension-installer: ^1.0
- phpstan/phpstan: ^1.8.1
- phpstan/phpstan-doctrine: 1.3.40
- phpstan/phpstan-strict-rules: ^1.3.0
- phpstan/phpstan-webmozart-assert: ^1.2.0
- phpunit/phpunit: ^9.5
- polishsymfonycommunity/symfony-mocker-container: ^1.0
- sylius-labs/coding-standard: ^4.2
- symfony/browser-kit: ^5.4 || ^6.0
- symfony/debug-bundle: ^5.4 || ^6.0
- symfony/dotenv: ^5.4 || ^6.0
- symfony/flex: ^2.2.2
- symfony/intl: ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^5.4 || ^6.0
- vimeo/psalm: 4.30.0
README
The Broken Link Handler plugin automatically redirects outdated product and taxon slugs to their new versions, ensuring they remain permanent and preventing their reuse by other entities.
The goal is to optimize your site so that search engines can index it more effectively, which will help improve its organic search ranking (SEO).
Presentation
Whenever you create or update a product's slug, it will be automatically saved in the product's slug history.
A slug that has already been used for a product can never be reused for another. This ensures that old slugs will always remain associated with the same product, so that when accessing an old slug, the user will be redirected to the new one.
For example, if someone tries to access the URL /fr_FR/products/000f-grey-jeans
, a 301 redirect will automatically lead to /fr_FR/products/000f-v2-jean-gris
.
This slug management system is also implemented for taxons. Thus, when attempting to access the page /fr_FR/taxons/t-shirts/les-hommes
, a redirection will occur to /fr_FR/taxons/t-shirts/hommes
.
Installation
Require plugin with composer :
composer require abenmada/sylius-broken-link-handler-plugin
Change your config/bundles.php
file to add the line for the plugin :
<?php return [ //.. Abenmada\BrokenLinkHandlerPlugin\BrokenLinkHandlerPlugin::class => ['all' => true], ]
Then create the config file in config/packages/abenmada_broken_link_handler_plugin.yaml
:
imports: - { resource: "@BrokenLinkHandlerPlugin/Resources/config/services.yaml" }
Copy the view responsible for displaying the taxon creation form :
mkdir -p templates/bundles/SyliusAdminBundle/Taxon cp vendor/abenmada/sylius-broken-link-handler-plugin/src/Resources/views/Admin/Taxon/_form.html.twig templates/bundles/SyliusAdminBundle/Taxon/_form.html.twig
Update the entity src/Entity/Product/Product.php
:
<?php declare(strict_types=1); namespace App\Entity\Product; use Abenmada\BrokenLinkHandlerPlugin\Model\SlugHistory\ProductSlugHistoryTrait; use Abenmada\BrokenLinkHandlerPlugin\Validator\SlugExistsInOtherProductSlugHistories; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Core\Model\Product as BaseProduct; use Symfony\Component\Validator\Mapping\ClassMetadata; /** * @ORM\Entity * @ORM\Table(name="sylius_product") */ #[ORM\Entity] #[ORM\Table(name: 'sylius_product')] class Product extends BaseProduct { use ProductSlugHistoryTrait; public function __construct() { $this->productSlugHistories = new ArrayCollection(); parent::__construct(); } public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addConstraint(new SlugExistsInOtherProductSlugHistories([ 'groups' => 'sylius', ])); } }
Update the entity src/Entity/Taxonomy/Taxon.php
:
<?php declare(strict_types=1); namespace App\Entity\Taxonomy; use Abenmada\BrokenLinkHandlerPlugin\Model\SlugHistory\TaxonSlugHistoryTrait; use Abenmada\BrokenLinkHandlerPlugin\Validator\SlugExistsInOtherTaxonSlugHistories; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; use Sylius\Component\Core\Model\Taxon as BaseTaxon; use Symfony\Component\Validator\Mapping\ClassMetadata; /** * @ORM\Entity * @ORM\Table(name="sylius_taxon") */ #[ORM\Entity] #[ORM\Table(name: 'sylius_taxon')] class Taxon extends BaseTaxon { use TaxonSlugHistoryTrait; public function __construct() { $this->taxonSlugHistories = new ArrayCollection(); parent::__construct(); } public static function loadValidatorMetadata(ClassMetadata $metadata): void { $metadata->addConstraint(new SlugExistsInOtherTaxonSlugHistories([ 'groups' => 'sylius', ])); } }
Run the migration :
bin/console doctrine:migration:migrate