• Stars
    star
    158
  • Rank 237,131 (Top 5 %)
  • Language
    PHP
  • Created about 1 year ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Laravel package that allows you to define self-healing URLs for Eloquent models

Self Healing URLs

Self Healing URLs is a simple Laravel package inspired by this video from Aaron Francis.

It allows you to mark Eloquent models as self-healing so that the URLs generated for said models can include an SEO friendly slug whilst not breaking should the slug alter in any way.

Installation

The package can be installed via Composer:

composer require lukeraymonddowning/self-healing-urls

Once installed, add the HasSelfHealingUrls trait to any Eloquent model:

use Lukeraymonddowning\SelfHealingUrls\Concerns\HasSelfHealingUrls;

class Post extends Model
{
    use HasSelfHealingUrls;
}

If your model has a column named slug, you're all set. Otherwise, define a $slug property on your model to inform the package which column to use instead:

use Lukeraymonddowning\SelfHealingUrls\Concerns\HasSelfHealingUrls;

class Post extends Model
{
    use HasSelfHealingUrls;
    
    protected $slug = 'title';
}

Don't worry if your "slug" isn't URL friendly; the package will take care of formatting it for you. In fact, it doesn't even have to be unique because the defined unique identifier for your model will also be included at the end.

Limitations

By default, the package requires that your unique identifier (such as the id or uuid column) not have any - characters. You can implement your own IdentifierHandler as detailed in the next section.

Unless you implement a custom Rerouter, the package requires that you have defined names to the routes you want to use with self healing URLs.

Using a custom IdentifierHandler

If you need to customize how a slug is joined to a model identifier (which by default is just a hyphen), you can create your own class implementing IdentifierHandler and register it in the register method of your AppServiceProvider.

Here is an example using an _ instead of a hyphen:

class UnderscoreIdentifierHandler implements IdentifierHandler
{
    public function joinToSlug(string $slug, string|int $identifier): string
    {
        return "{$slug}_{$identifier}";
    }

    public function separateFromSlug(string $value): string
    {
        return Str::afterLast($value, '_');
    }
}

Register the custom handler in your AppServiceProvider like so:

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(IdentifierHandler::class, UnderscoreIdentifierHandler::class);
    }
}

Attributions

Without Aaron's video, I wouldn't have even thought about this, so props to him. Go watch the video.

More Repositories

1

whenipress

A tiny, powerful and declarative wrapper around keyboard bindings in JavaScript
JavaScript
481
star
2

honey

A spam prevention package for Laravel, providing honeypot techniques, ip blocking and beautifully simple Recaptcha integration. Stop spam. Use Honey.
PHP
407
star
3

poser

Create class based model factories in Laravel applications in seconds.
PHP
277
star
4

MountainBreeze

A Wordpress theme starter template for the modern web developer, including Tailwind CSS, Alpine JS and Laravel Blade
PHP
163
star
5

mula

A Laravel package that makes working with money in a secure manner a cinch!
PHP
153
star
6

alpinimations

Streamline your animations by using these simple blade directives in your components!
Blade
101
star
7

laravel-template

An opinionated Laravel setup using my favourite tools
PHP
59
star
8

actions-are-a-devs-best-friend

Talk codebase
PHP
26
star
9

pest-plugin-money

A plugin for working with popular money libraries in Pest
PHP
19
star
10

Tupper

A simple, no-nonsense IoC Container written in PHP for Dependency Injection
PHP
13
star
11

nightguard

Set up Laravel Auth guards using Eloquent in seconds
PHP
11
star
12

pest-plugin-larastrap

Wraps your Pest suite in a Laravel application instance, allowing global use of the framework in tests.
PHP
9
star
13

uniquestyles

PostCSS plugin that strips selectors found in other stylesheets.
JavaScript
9
star
14

laracasts-pest-from-scratch

PHP
5
star
15

infinite_scrolling_in_inertia_js

PHP
3
star
16

laravel_nova_mastery_2023

The Source Code for the Laravel Nova Mastery 2023 Series
PHP
3
star
17

stubble

A templating engine for Laravel stub files
PHP
2
star
18

valid-variants-of-validating-validation

The presentation, code and resources for the talk "Valid Variants of Validating Validation"
PHP
2
star
19

migrating-from-mix-to-vite

JavaScript
2
star
20

blog-2

My personal blog
PHP
2
star
21

laravel-erp-crm

A Laravel ERP Module providing CRM functionality
PHP
1
star
22

syndicate

A simple way to add companies, organisations and groups to your Laravel project.
PHP
1
star
23

plug-in-play

PHP
1
star
24

laravel-address-lookup

Allows you to use many different services to find an address based on a postal code.
PHP
1
star
25

blog

My personal blog
PHP
1
star