Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Self Healing Urls

lukeraymonddowning/self-healing-urls image

Self Healing Urls stats

Downloads
29
Stars
76
Open Issues
0
Forks
4

View on GitHub →

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.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Lukeraymonddowning Self Healing Urls Related Articles

Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow image

Lens for Laravel Brings WCAG Auditing to Your Local Dev Workflow

Read article
Livewire or Inertia.js? image

Livewire or Inertia.js?

Read article
Building a Vue SPA with Laravel image

Building a Vue SPA with Laravel

Read article
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup