PHP code example of wisoft / slug-history-bundle

1. Go to this page and download the library: Download wisoft/slug-history-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wisoft / slug-history-bundle example snippets


Wisoft\SlugHistoryBundle\SlugHistoryBundle::class => ['all' => true],

use Wisoft\SlugHistoryBundle\Attribute\Slugged;

class Product
{
    #[Slugged(
        from: 'title',
        routeName: 'app_product_show',
        routeParams: [
            'category' => '@category',
            'slug' => '@slug',
            'source' => 'legacy'
        ]
    )]
    private ?string $slug = null;
    private string $category = 'electronics';
}

class Product
{
    #[Slugged(
        from: 'title',
        routeName: 'app_product_show',
        routeParams: [
            'category' => '@category',
            'source' => 'legacy'
            // 'slug' => '@slug' is added automatically
        ]
    )]
    private ?string $slug = null;
    private string $category = 'electronics';
}

use Wisoft\SlugHistoryBundle\Form\Type\SluggedType;

$builder->add('slug', SluggedType::class, [
    'route' => [
        'name' => 'app_product_show',
        'params' => [
            'category' => '@category',
            'source' => 'legacy'
            // 'slug' parameter is auto-detected from the field name
        ]
    ],
    'from' => 'title',
    'showLabel' => 'View the page',
]);

$builder->add('productSlug', SluggedType::class, [
    'route' => [
        'name' => 'app_product_show',
        'slugParam' => 'slug',  // route expects 'slug', but field is 'productSlug'
        'params' => [
            'category' => '@category',
        ]
    ],
    'from' => 'title',
]);

use Wisoft\SlugHistoryBundle\Service\Storage\SlugStorageInterface;
use Wisoft\SlugHistoryBundle\Service\Storage\CacheSlugStorage;

$services->alias(SlugStorageInterface::class, CacheSlugStorage::class);

use Wisoft\SlugHistoryBundle\Service\Storage\SlugStorageInterface;
use Wisoft\SlugHistoryBundle\Service\Storage\DatabaseSlugStorage;

$services->alias(SlugStorageInterface::class, DatabaseSlugStorage::class);

class Product
{
    private Company $company;
    private string $slug;

    public function getCompany(): Company { return $this->company; }
    public function getSlug(): string { return $this->slug; }
}

class Company
{
    private string $name;

    public function getName(): string { return $this->name; }
}

#[Slugged(
    routeParams: [
        'company' => '@company.name',      // resolves getCompany()->getName()
        'slug' => '@slug',                 // resolves getSlug()
    ],
    routeName: 'app_product_show',
)]
private ?string $slugField = null;
bash
php bin/console asset:install
bash
php bin/console asset:install
bash
php bin/console make:migration
php bin/console doctrine:migrations:migrate