GithubHelp home page GithubHelp logo

jeffersonsimaogoncalves / self-healing-urls Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lukeraymonddowning/self-healing-urls

0.0 0.0 0.0 68 KB

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

PHP 100.00%

self-healing-urls's Introduction

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.

self-healing-urls's People

Contributors

lukeraymonddowning avatar imanghafoori1 avatar alexjustesen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.