Download the PHP package whitecube/laravel-sluggable without Composer
On this page you can find all versions of the php package whitecube/laravel-sluggable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download whitecube/laravel-sluggable
More information about whitecube/laravel-sluggable
Files in whitecube/laravel-sluggable
Package laravel-sluggable
Short Description Generate slugs for your models automatically - even with translated attributes
License MIT
Homepage https://github.com/whitecube/laravel-sluggable
Informations about the package laravel-sluggable
Laravel Sluggable
A trait to use on your models to generate slugs based on another attribute's value. Supports translated attributes (using spatie/laravel-translatable).
Installation
You can install the package via composer:
Usage
Changing the slug storage attribute
By default, the slug is configured to be stored in an attribute named slug
. You can overwrite this setting with the public $slugStorageAttribute
property on your model.
Conditional sluggable attributes
If needed, you can overwrite the trait's getSluggable()
method and put your own sluggable attribute choice logic in it:
Translated slugs
You can generate slugs based on translated attributes (using spatie/laravel-translatable). Remember to add the slug
attribute to the public $translatable
array to easily access them.
Route Model Binding
Be advised that this package overrides the getRouteKeyName
method, which means Laravel's Route Model Binding will use the slug attribute by default (or the $slugStorageAttribute
you have defined). In most cases, this is great, saves you a step and cleans up your models, but if you must, you can change it to whatever you like.
When using Route Model Binding, some other interesting features become available.
Custom Route Model Binding Query
Sometimes you'd want to add custom behavior to the query Laravel performs. This packages makes it possible to hook into the query by defining a getRouteBindingQuery
method on the model, which could be useful in several cases such as including soft-deleted results :
Cross-language redirects
If the slug provided in the URL does not correspond to the current locale's slug translation, but corresponds to a slug in another language, this package can automatically redirect to the proper slug.
An example: given the above example's post, we could imagine the following routing configuration:
But if we visit /fr/articles/the-title
, the package will automatically perform a 301
redirect to /fr/articles/le-titre
.
This behavior can be disabled by setting public $slugTranslationRedirect = false;
on your model, in which case visiting /fr/articles/the-title
will just render a 404
page.
Translated URL generator
Do you need to generate alternate locale links for a translated sluggable model? We've got you covered. Take a look at getSluggedUrlForRoute($route, $locale, $fullUrl)
:
Considering the current route being /blog/{post}/{tab}
with request parameter {tab} = 'comments'
, the result would be:
💖 Sponsorships
If you are reliant on this package in your production applications, consider sponsoring us! It is the best way to help us keep doing what we love to do: making great open source software.
Contributing
Feel free to suggest changes, ask for new features or fix bugs yourself. We're sure there are still a lot of improvements that could be made, and we would be very happy to merge useful pull requests.
Thanks!
Made with ❤️ for open source
At Whitecube we use a lot of open source software as part of our daily work. So when we have an opportunity to give something back, we're super excited!
We hope you will enjoy this small contribution from us and would love to Twitter for more updates!
All versions of laravel-sluggable with dependencies
illuminate/support Version ^9.0|^10.0|^11.0
laravel/helpers Version ^1.1