andrey-helldar/short-url

This package is abandoned and no longer maintained. The author suggests using the ashallendesign/short-url package instead.

A simple short URL generator for Laravel Framework.

v2.2.0 2021-03-23 15:05 UTC

This package is auto-updated.

Last update: 2021-06-02 11:35:29 UTC


README

A simple short URL generator for Laravel Framework.

StyleCI Status Github Workflow Status Coverage Status Scrutinizer Code Quality For Laravel

Stable Version Unstable Version Total Downloads License

This package is abandoned and no longer maintained. The author suggests using the ashallendesign/short-url package instead.

Installation

To get the latest version of Laravel Short URL, simply require the project using composer:

composer require andrey-helldar/short-url

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "andrey-helldar/short-url": "^2.0"
    }
}

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php:

Helldar\ShortUrl\ServiceProvider::class;

You can also publish the config file to change implementations (ie. interface to specific class):

php artisan vendor:publish --provider="Helldar\ShortUrl\ServiceProvider"

Now you can use a Helldar\ShortUrl\Facades\ShortUrl facade.

Using

Identifiers

When creating records, there are two ways to create unique identifiers: The first one is by the record identifier in the database with its conversion into a letter index:

id key url output url
1 b https://example.com https://my-site.com/go/b
2 c https://example.com/foo https://my-site.com/go/c
3 d https://example.com/bar https://my-site.com/go/d
... ... ... ...
100 dw https://example.com/baz https://my-site.com/go/dw
200 hs https://example.com/qwe https://my-site.com/go/hs
... ... ... ...
1000 bmm https://example.com/rty https://my-site.com/go/bmm
5000 hki https://example.com/qax https://my-site.com/go/hki
... ... ... ...

The second is unique identifier based on the current time in microseconds:

id key url output url
1 5d35b1610705f679245100 https://example.com https://my-site.com/go/5d35b1610705f679245100
2 5d35b1727eb33156257300 https://example.com/foo https://my-site.com/go/5d35b1727eb33156257300
3 5d35c3193aaf8239852915 https://example.com/bar https://my-site.com/go/5d35c3193aaf8239852915
... ... ... ...
100 5d35c3195eaaa426635669 https://example.com/baz https://my-site.com/go/5d35c3195eaaa426635669
200 5d35c3199d7f7453462663 https://example.com/qwe https://my-site.com/go/5d35c3199d7f7453462663
... ... ... ...
1000 5d35c31a44398568656171 https://example.com/rty https://my-site.com/go/5d35c31a44398568656171
5000 5d35c31aca1c1975257906 https://example.com/qax https://my-site.com/go/5d35c31aca1c1975257906
... ... ... ...

set()

To create a record, use the method set():

use \Helldar\ShortUrl\Facades\ShortUrl;

$item = ShortUrl::set('https://example.com');
// returned instance of `Helldar\ShortUrl\Models\Short` eloquent model.

get()

To get record, use the method get():

use \Helldar\ShortUrl\Facades\ShortUrl;

$url = ShortUrl::get('foo');
// returned URL string.
// For example, 'http://<your_site>.com/go/qdr'.

routing

To create a route you can use the following code:

use \Helldar\ShortUrl\Facades\ShortUrl;

$item = ShortUrl::set('https://example.com');

return route('short_url', ['key' => $item->key]); 

or

use \Helldar\ShortUrl\Facades\ShortUrl;

$url = ShortUrl::get('foo');

return redirect()->away($url); 

You can also change the name of the route in the package settings.

Blade templates

You can also call the facade from the template engine:

<a href="{{ \Helldar\ShortUrl\Facades\ShortUrl::get('foo') }}">open link</a>

{{-- <a href="http://your-site.com/go/foo">open link</a> --}}

or

<a href="{{ \Helldar\ShortUrl\Facades\ShortUrl::set('http://example.com') }}">open link</a>

{{-- <a href="http://your-site.com/go/cr">open link</a> --}}

License

This package is licensed under the MIT License.

For Enterprise

Available as part of the Tidelift Subscription.

The maintainers of andrey-helldar/short-url and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. Learn more.