Download the PHP package fbeen/uniqueslugbundle without Composer
On this page you can find all versions of the php package fbeen/uniqueslugbundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fbeen/uniqueslugbundle
More information about fbeen/uniqueslugbundle
Files in fbeen/uniqueslugbundle
Package uniqueslugbundle
Short Description Add easy and quickly unique slugs to your enities and routes
License MIT
Homepage https://github.com/Fbeen/UniqueSlugBundle
Informations about the package uniqueslugbundle
FbeenUniqueSlugBundle for Symfony 4
With this bundle you will be able to automatic generate unique slugs inside your entities by simply add a @Slug annotation to a specified field.
Features include:
- very simple installation
- generate unique slugs from one or more properties or a custom method in an entity
- automatic storage into the database with always a unique slug
- making the slug unique by adding digits when necessary
- add or regenerate slugs in existing tables with the command prompt
- support for date, time and datetime fields with custom format
- supports custom (language specific) slugifiers
Requirements
This bundles current release requires Symfony 4. You can use Version 1.2 from this Bundle to use it with Symfony >= 2.7
Known restrictions
- The slugs will be generated when you persist your entity to the database which means that when you create your entity or update properties the slug is not yet available or updatet.
- Slugs cannot made from the entities primary parameter (@Id) because those values will be set by the database INSERT command.
- The slugs will be truncated to the length of the slug field minus 8(*) characters! the last positions are used for additional digits that will make the slug unique if necessary.
(*) 8 at default conifguration or otherwise as much as the 'fbeen_unique_slug.maximum_digits' parameter (see below at "Full configuration example")
Installation
1. Run composer:
2. Adding Slug behavior to your entity:
Suppose that you have a Newsitem entity to display some news on your website and that you want to generate slugs from the $title property.
You might then have an entity like this:
Important notes when you edit your entity manually:
- Do not forget the
use Fbeen\UniqueSlugBundle\Annotation\Slug;
- Add a
@Slug("title")
annotation to the slug property to tell the application it should create a slug from the $title property - Add a constructor that sets the $created property to the current date and time
- use the following commands on the console to add the getters and setters and to update the database:
3. Migrate:
Apply the changes to the database:
4. Using the Slugs in your routes
From now on if you persist your entity the slug will be automatically generated. To use the slugs into the routes you could simply use the $slug property into the route e.g.
And then you have to retrieve the right newsitem using the given slug. Fortunately thanks to Symfony's automatic parameter conversion this is as easy as this:
Don't forget to pass the slug property to the router when you generate a route to your show action:
Advanced Slugs
The slug annotation has some more futures:
1) To generate slugs from more than one property just write an array of properties: @Slug({"created", "title"})
2) To add your own format for date, time and datetime fields use the format parameter: @Slug({"created", "title"}, format="Y-m-d")
.
3) You could also write your own method that builds the slug and apply the method name to the @Slug annotation.
Update the slugs for all records of your table
Using the Symfony console commands you are able to generate the slugs for all the records:
Create your custom (language specific) slugifier
-
First write your own slugifier class and be sure that your class implements the
Fbeen\UniqueSlugBundle\Slugifier\SlugifierInterface
e.g. -
If you place your slugifier in the App\Service directory it will be autowired as a service. If not add a service definition in your /config/services.yaml file
-
Add a configuration file in the config/packages directory with the name fbeen_unique_slug.yaml.
- Now you should run phpunit Tests/SlugifierTest in the main directory of this bundle to test your slugifier class! (you must have phpunit installed. See https://phpunit.readthedocs.io/en/8.0/installation.html)
Ready! From now on the slugs will be generated with your own slugifier class.
Full configuration example with default values
All versions of uniqueslugbundle with dependencies
ext-intl Version *
doctrine/inflector Version ^1.2
nikic/php-parser Version ^4.0
symfony/config Version ^3.4|^4.0
symfony/console Version ^3.4|^4.0
symfony/dependency-injection Version ^3.4|^4.0
symfony/filesystem Version ^3.4|^4.0
symfony/finder Version ^3.4|^4.0
symfony/framework-bundle Version ^3.4|^4.0
symfony/http-kernel Version ^3.4|^4.0
symfony/maker-bundle Version >1.1