Pixelairport's avatar

Package for custom model urls in Laravel?

Hi. Im thinking about creating a package, but wants to know, if there is already something like this at github. For example I have a project/35 url (project/{id}), which is the project with ID 35. I know I could use slugs. For example with the title. But titles are often to long. So I want to have a field for "custom slug/custom domain". This would be simple. I would also do validation, that the custom domain is only set one. But I there must be also 301 Redirects, if custom url is changed. So the domain entry must have relation to project model and all other must redirect to current active domain with 301. And of course for example the domain "main-item" could be as "project/main-item" and "page/main-item". So "main-item" must be unique. But not unique for all, only unique for each single model.

Is there a package who does this. Or something like this? Maybe somebody knows something? thx

0 likes
5 replies
LaryAI's avatar
Level 58

Unfortunately, I'm not aware of any existing packages that provide this functionality. However, you can create your own package to achieve this. You can start by creating a model that will store the custom URLs. This model should have a relationship with the project model, so that you can store the custom URL for each project. You can then create a middleware that will check if the requested URL matches any of the custom URLs stored in the database. If it does, you can redirect the user to the correct project page.

// CustomUrl model

class CustomUrl extends Model
{
    public function project()
    {
        return $this->belongsTo(Project::class);
    }
}

// CustomUrl middleware

class CustomUrlMiddleware
{
    public function handle($request, Closure $next)
    {
        $customUrl = CustomUrl::where('url', $request->path())->first();
        if ($customUrl) {
            return redirect()->route('project.show', $customUrl->project);
        }

        return $next($request);
    }
}
Pixelairport's avatar

thx @rodrigo.pedra I think i have to create it by my own. This are only 50% of what the plugins should do. I build a cms and users should also create their own urls in the backend.

1 like
rodrigo.pedra's avatar

@Pixelairport if you want some kick-off, this package by Spatie, have the redirection logic injected on the router, and allows you to write your custom redirector class:

https://packagist.org/packages/spatie/laravel-missing-page-redirector

So you are only to be worried with your custom database logic. I guess you could either add it as your package's dependency, or publish just a custom redirector and tell users they need both packages, and how to configure them.

1 like
rodrigo.pedra's avatar

@Pixelairport

I never used the first two I linked. The latter, from Spatie I've already used, but with configuration redirects as it defaults to.

But, from what I skimmed on the first two, at least on the first, you can plug a database model to provide the redirects. Maybe it is worth taking a closer look.

Please or to participate in this conversation.