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

 SPATIE  

  Laravel Sluggable Go 
=======================

spatie.be/open-source

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

 Version   v1      

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

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

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

- [ Generating your first slug ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/getting-started)
- [ Using the Sluggable attribute ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/using-the-attribute)
- [ Using the HasSlug trait ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/using-the-has-slug-trait)
- [ Finding models by slug ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/finding-models-by-slug)
- [ Self-healing URLs ](https://spatie.be/docs/laravel-sluggable-go/v1/basic-usage/self-healing-urls)

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

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

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

###  On this page 

1. [ Publishing the config ](#content-publishing-the-config)
2. [ Example: uppercase slugs in the route key ](#content-example-uppercase-slugs-in-the-route-key)
3. [ Example: putting the identifier first ](#content-example-putting-the-identifier-first)

The package delegates low-level work to actions that can each be swapped for your own.

- `Options[T].Generate` replaces the complete generation action for a model.
- `Config.Slugify` replaces source-to-slug conversion.
- `Config.BuildRouteKey` composes the self-healing route key.
- `Config.ExtractIdentifier` reads the identifier from an incoming route key.

Publishing the config
-----------------------------------------------------------------------------------------------------------------------

Go has no published PHP config file or subclass names. Configure callbacks when registering `sluggable.New`. Each application receives fresh actions on registration. Do not register a second service of the same type to override these actions.

```
sluggable.New(func(config *sluggable.Config) {
    config.Slugify = func(source, separator, language string) string {
        return strings.ToUpper(sluggable.Slugify(source, separator, language))
    }
})
```

`Options.Generate` receives the context, pending model, persisted original or nil on create, and options. It must update the pending model without saving it. Return an error to abort the surrounding write. Use `Source`, `SkipWhen`, `Scope` or `Suffix` when only that part needs to change.

Example: uppercase slugs in the route key
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Wrap the default build action:

```
sluggable.New(func(config *sluggable.Config) {
    config.BuildRouteKey = func(slug, identifier, separator string) string {
        return sluggable.BuildRouteKey(strings.ToUpper(slug), identifier, separator)
    }
})
```

Example: putting the identifier first
---------------------------------------------------------------------------------------------------------------------------------------------------------------------

The default route key looks like `hello-world-5`. To flip it to `5--hello-world`, override both self-healing actions: one to build the new format, one to parse it back into an identifier.

```
sluggable.New(func(config *sluggable.Config) {
    config.BuildRouteKey = func(slug, identifier, separator string) string {
        return identifier + separator + slug
    }
    config.ExtractIdentifier = func(value, separator string) (string, bool) {
        identifier, _, found := strings.Cut(value, separator)
        return identifier, found && identifier != ""
    }
})
```

Set the model's `SelfHealingSeparator` to `"--"`. Pick a separator that does not occur inside the primary key. Numeric IDs and ULIDs do not contain hyphens. UUIDs contain single hyphens.

A post with id `5` and slug `hello-world` now exposes `5--hello-world` as its route key. A stale key like `5--old-title` still resolves the post and redirects to the canonical URL.

 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)
