Sluggable for Laravel Rust
laravel-sluggable is a port of cviebrock/eloquent-sluggable to Laravel Rust. It generates unique slugs for your models when they are created or updated.
[!NOTE] This is a community port maintained by Rivervo. It is not an official package of Colin Viebrock. It follows cviebrock/eloquent-sluggable 13.0.2: the same settings, defaults, uniqueness rules and suffixes, with Rust naming. Slugs are made by a port of cocur/slugify 4.8.0 with its own transliteration rules, so they match the PHP package's character for character. All credit for the design goes to Colin Viebrock and the package's contributors.
- Installation
- Making a model sluggable
- Settings
- Updating models
- Custom slug engine
- Unique slugs scoped to a query
- SlugService
- Finding models by slug
- Testing
- Differences from PHP
- Not yet ported (planned for a later release)
- License
Installation
cargo add laravel-sluggable
Register the service provider in src/app.rs, which adds the settings section:
use SluggableServiceProvider;
configure
.with_providers
Making a model sluggable
Give the model a slug column, implement Sluggable and add the hooks:
use model;
use ;
sluggable_hooks!;
let post = create!.await?;
// post.slug == Some("my-awesome-blog-post")
A slug column is a String or Option<String> column. Call source several times to build the slug from several columns, joined with spaces as in PHP, or use source_with for a computed source.
If your model already has Hooks, call the package from them instead of using sluggable_hooks!:
A model can have several slugs: return several Slugs.
Settings
Each Slug method is one of PHP's per-model settings. A setting the model doesn't set comes from the SluggableConfig section, PHP's config/sluggable.php, with the same defaults:
| PHP setting | Slug method |
SluggableConfig field |
Default |
|---|---|---|---|
source |
source, source_with |
||
maxLength |
max_length |
max_length |
None |
maxLengthKeepWords |
max_length_keep_words |
max_length_keep_words |
true |
method |
method |
method |
None (the slug engine) |
separator |
separator |
separator |
- |
unique |
unique |
unique |
true |
uniqueSuffix |
unique_suffix |
unique_suffix |
None (-2, -3, …) |
firstUniqueSuffix |
first_unique_suffix |
first_unique_suffix |
2 |
reserved |
reserved, reserved_with |
reserved |
None |
onUpdate |
on_update |
on_update |
false |
slugEngineOptions |
slug_engine_options |
slug_engine_options |
cocur/slugify's defaults |
new
.source
.max_length
.separator
.reserved
.unique_suffix
Change the defaults for every model with with_config:
configure
.with_config
Updating models
As in PHP, a slug is generated when the model is created and the slug is empty. An update keeps the slug unless the slug is emptied or on_update(true) is set. A slug you set yourself is kept.
Custom slug engine
Override customize_slug_engine to add rules or rulesets for a slug column:
Unique slugs scoped to a query
Override with_unique_slug_constraints to limit uniqueness, for example to the same author:
find_similar_slugs (PHP's scopeFindSimilarSlugs) can be overridden the same way.
SlugService
// A unique slug without saving anything: SlugService::createSlug
let slug = .await?;
let slug = .await?;
// Regenerate a saved model's slugs in memory: SlugService::slug($model, $force)
slug.await?;
Finding models by slug
Implement SluggableScopeHelpers (PHP's trait of the same name):
let post = find_by_slug.await?;
let post = find_by_slug_or_fail.await?; // ModelNotFound, a 404 in a handler
let posts = where_slug.get.await?;
The primary slug is the first one; override slug_key_name to pick another, as PHP's $slugKeyName.
Testing
cargo test --all-features
The tests port the PHP package's BaseTests, UniqueTests, OnUpdateTests, StaticTests and ScopeHelperTests where they map, on SQLite.
Differences from PHP
- Settings are a
Slugbuilder and aSluggableConfigsection instead of arrays;sourcetakes typed columns. - PHP hooks into the model's
savingevent. Here the model'sHookscall the package (sluggable_hooks!does it for you). - A custom
uniqueSuffixclosure receives the similar slugs as a list, without their model keys. - A
reservedslug's suffix never treats the reserved list's index as the model's key (a PHP quirk). strip_tagsin the slug engine removes<...>tags only; PHP'sstrip_tagshandles more edge cases.
Not yet ported (planned for a later release)
- The
sluggingandsluggedevents (the first one can cancel slugging). - The
savedevent (sluggableEvent()returningSAVED), needed when the primary key is part of the source. With the defaultsavingbehaviour the key isn't known on create, as in PHP. replicate()re-slugging the copy: use..post.replicate().except(Post::slug)so the copy gets a new slug.- Soft deletes and
includeTrashed: Laravel Rust has no soft deletes yet. - PHP sources that name a relation's attribute (
author.name); usesource_withwith a value you have.
License
MIT. See LICENSE.md.