ash-jc-allen / short-url

A Laravel package for creating shortened URLs for your web apps.
MIT License
1.27k stars 162 forks source link

Suggestion: Make the base URL configurable and prefix optional #113

Closed maxacarvalho closed 2 years ago

maxacarvalho commented 2 years ago

Hello,

Thanks for your solution, it's very nice.

I'd like to make a suggestion, to let the application users to choose the base URL and make the prefix optional.

At the moment, I can see that the default_sort_url is set to config('app.url').'/'.$this->prefix().'/'.$this->urlKey, which makes impossible to have a domain different than the application one for the shortened URL.

Use case:

Let's say I have a full domain app that's https://full-domain-app.com, but when shortening the URL, I want to use https://fda.is, so the URL is even shorter. Let's say I also want to remove the prefix altogether.

Something like this could do the trick:

...
    protected function insertShortURLIntoDatabase(): ShortURL
    {
        $baseUrl = $this->baseUrl ?? config('app.url');

        $prefix = $this->prefix() ? "{$this->prefix()}/" : '';

        return ShortURL::create([
            'destination_url'                => $this->destinationUrl,
            'default_short_url'              => "{$baseUrl}/{$prefix}".$this->urlKey,
...

I could create a PR for that, but I think it's better to understand if that's something you would like to add to your package.

Thanks!

ash-jc-allen commented 2 years ago

Hey @maxacarvalho! I 100% agree that I think that would be a really useful feature. I've actually been working on a project myself that would probably benefit from this type of functionality in the future.

I'm more than happy to accept a PR for this functionality if you fancy making one 😄