marjose/url-shortener

This will generate a shorten URL based on the supplied URL

Fund package maintenance!
marjose

v1.0.0 2021-09-28 07:04 UTC

This package is auto-updated.

Last update: 2024-10-29 06:12:57 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require marjose/UrlShortener

You can publish and run the migrations with:

php artisan vendor:publish --provider="Marjose\UrlShortener\UrlShortenerServiceProvider" --tag="UrlShortener-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --provider="Marjose\UrlShortener\UrlShortenerServiceProvider" --tag="UrlShortener-config"

This is the contents of the published config file:

return [
    'length' => 6, // Length of the string for shortening the URL
    'expiration' => 120, // minutes, 0 for no expiration
];

Usage

Generating the URL by using the default setting from the Config file.

use Marjose\UrlShortener\Facades\UrlShortener;
$response = UrlShortener::url('https://xxxx.xx/xxxx')->generate();

Generating the URL by adding expiration to the link.

use Marjose\UrlShortener\Facades\UrlShortener;
$response = UrlShortener::url('https://xxxx.xx/xxxx')->setExpiration(15)->generate();

Generating the URL by overriding the default setting from the Config file.

use Marjose\UrlShortener\Facades\UrlShortener;
$response = UrlShortener::url('https://xxxx.xx/xxxx')->setLength(5)->setExpiration(15)->generate();

Validate if the shorted URL is expired or not

use Marjose\UrlShortener\Facades\UrlShortener;
$response = UrlShortener::url('https://xxxx.xx/xxxx')->isExpired();

// This will return boolean or an exception if the supplied url is not found in the database

In your web.php route you can add a new route similar to this, so that you can capture and redirect to the original link once the like is validated by your controller.

Route::get('s/{url_key}',[YourControllerHere::class, 'YourControllerMethod']);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.