ash-jc-allen / short-url

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

any way to have multiple short base urls? #251

Closed goellner closed 5 months ago

goellner commented 5 months ago

I have set up a custom default_url in the config, now i need to override it, to generate a short link with a different base url than the one in the config.

Tried overwriting the config and failed with this:

config(['short-url.default_url' => 'https://link.asdf.com']);

Is there any way to override at runtime?

full context of my example:

$og_default_url = config('short-url.default_url');

// change default_url config to new one temporarily
config(['short-url.default_url' => config('app.deep_link_url')]);

$builder = new Builder();

$builder = $builder
    ->destinationUrl($destination_url_link)
    ->urlKey($url_key)
    ->redirectStatusCode(302);

// revert default_url config
config(['short-url.default_url' => $og_default_url]);
ash-jc-allen commented 5 months ago

Hey @goellner! Sorry to hear that you're having problems with the package. I've just tried running your code in the example and just want to check something.

I don't know if your example above is exactly the same as the code in your project, but in your example, you're not calling the make method on the Builder class. The Short URL won't be created until you've called that method.

Is it possible that you're calling make after you've switched your config back to the original values?

I've updated your example slightly and this seems to work:

$og_default_url = config('short-url.default_url');

// change default_url config to new one temporarily
config(['short-url.default_url' => config('app.deep_link_url')]);

$builder = new Builder();

$shortUrl = $builder
    ->destinationUrl($destination_url_link)
    ->urlKey($url_key)
    ->redirectStatusCode(302)
    ->make();

// revert default_url config
config(['short-url.default_url' => $og_default_url]);

dd($shortUrl);

In the example above, I've just added make to the end of the chain. Does adding this on your end solve your issue? 😄

goellner commented 5 months ago

That was it! Would not have thought of this, I called it after and then the config was already reset. Thank you!