Overriding the underlying actions | laravel-sluggable-rust | Spatie       

 SPATIE  

  Laravel Sluggable 
====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-sluggable-rust](https://spatie.be/docs/laravel-sluggable-rust/v1)  Advanced-usage  Overriding the underlying actions

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-sluggable-rust/v1) 

  Overriding the underlying actions    
- [ Introduction ](https://spatie.be/docs/laravel-sluggable-rust/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-sluggable-rust/v1/requirements)
- [ Support us ](https://spatie.be/docs/laravel-sluggable-rust/v1/support-us)
- [ Installation and setup ](https://spatie.be/docs/laravel-sluggable-rust/v1/installation-setup)
- [ Translatable slugs ](https://spatie.be/docs/laravel-sluggable-rust/v1/translatable-slugs)
- [ Changelog ](https://spatie.be/docs/laravel-sluggable-rust/v1/changelog)
- [ Questions and issues ](https://spatie.be/docs/laravel-sluggable-rust/v1/questions-issues)
- [ Upgrading ](https://spatie.be/docs/laravel-sluggable-rust/v1/upgrading)
- [ Testing ](https://spatie.be/docs/laravel-sluggable-rust/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-sluggable-rust/v1/releasing)

Basic usage
-----------

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/getting-started)
- [ Using slug options ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/using-slug-options)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-rust/v1/basic-usage/self-healing-urls)

Advanced usage
--------------

- [ Combining multiple source columns ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/source-fields)
- [ Tuning the uniqueness suffix ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/uniqueness)
- [ Overriding the underlying actions ](https://spatie.be/docs/laravel-sluggable-rust/v1/advanced-usage/overriding-actions)

 Overriding the underlying actions
=================================

###  On this page 

1. [ Replacing slug generation ](#content-replacing-slug-generation)
2. [ Putting the identifier first ](#content-putting-the-identifier-first)

The package delegates slug generation and self-healing URL logic to replaceable actions. Set them in your application's `with_config` callback; application configuration runs after package defaults.

- `Config::generator` implements `SlugGenerator` and receives the complete generation request, including scoped query, destination, custom slug, and rules.
- `Config::route_keys` implements `RouteKeys` and controls both building and parsing composite keys.

Per-model `SlugOptions` remain application-owned. Source callbacks, skip callbacks, scope, transliteration and suffix generation can all be replaced without replacing the complete action.

Replacing slug generation
-----------------------------------------------------------------------------------------------------------------------------------

Implement `SlugGenerator::generate`. It returns a boxed, sendable future yielding `laravel::Result<String>`. Return an error to abort the model write. Delegate to `GenerateSlug` when you only want to alter part of the request:

```
use std::{future::Future, pin::Pin, sync::Arc};
use spatie_sluggable::{Config, GenerateSlug, Generation, SlugGenerator};

#[derive(Debug)]
struct PrefixedSlug;

impl SlugGenerator for PrefixedSlug {
    fn generate(&self, mut input: Generation)
        -> Pin<Box<dyn Future<Output = laravel::Result<String>> + Send + '_>>
    {
        input.source = format!("Article {}", input.source);
        Box::pin(async move { GenerateSlug.generate(input).await })
    }
}

config.section_mut::<Config>().generator = Arc::new(PrefixedSlug);
```

Putting the identifier first
--------------------------------------------------------------------------------------------------------------------------------------------

Implement both route-key operations together so formatting and parsing cannot drift apart:

```
use spatie_sluggable::RouteKeys;

#[derive(Debug)]
struct IdentifierFirst;

impl RouteKeys for IdentifierFirst {
    fn build(&self, slug: &str, id: &str, separator: &str) -> String {
        if slug.is_empty() { id.into() } else { format!("{id}{separator}{slug}") }
    }

    fn identifier(&self, value: &str, separator: &str) -> Option<String> {
        let id = value.split_once(separator).map_or(value, |(id, _)| id);
        (!id.is_empty()).then(|| id.into())
    }
}
```

Set `config.section_mut::<Config>().route_keys = Arc::new(IdentifierFirst)` in `with_config`. Use a separator that cannot appear in your identifiers. With UUIDs, for example, choose `--` instead of `-`.

To customize the stale-link response rather than its key format, register an application [error renderer](../basic-usage/self-healing-urls#customizing-the-redirect).

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
