A Laravel package that can be used for adding shortened URLs to your existing web app.
The package has been developed and tested to work with the following minimum requirements:
- PHP >=8.1
- Laravel >=9.0
You can install the package via Composer:
composer require orlovtech/short-link
You can then publish the package's config file and database migrations by using the following command:
php artisan vendor:publish --provider="OrlovTech\ShortLink\Providers\ShortLinkServiceProvider"
at this config file you can change the prefix you want to use. By default it's a /short/
.
This package contains one migration that add a new table to the database: short_links
. To run this migration, simply run the following command:
php artisan migrate
The fastest way to generate new link is to use the facade OrlovTech\ShortLink\Facades\ShortLink
like this:
ShortLink::generate('https://yourlink.com');
This method will return you short version of your link.
Method generate
also has the second parameter singleUse
as an option.
With this parameter you can specify that your link should be deleted after it was used for the first time.
So the full view might be:
ShortLink::generate(
'https://yourlink.com',
singleUse: true,
);
To show ready link use param default_short_url
like so:
$link = ShortLink::generate('https://yourlink.com');
echo config('app.url') . $link->default_short_url;
When you have ready link you can use the endpoint https://yourdomain.com/short/628ac418-865a
where short
is the prefix from config file
To run the package's unit tests, run the following command:
vendor/bin/phpunit
The MIT License (MIT). Please see License File for more information.