|
1 |
| -# laravel-url-shortener |
| 1 | +# Laravel URL Shortener |
2 | 2 | Powerful URL shortening tool using different drivers for your Laravel projects
|
| 3 | + |
| 4 | +## Table of Contents |
| 5 | + |
| 6 | +- [Overview](#overview) |
| 7 | +- [Installation](#installation) |
| 8 | + - [Requirements](#requirements) |
| 9 | + - [Install Package](#install-package) |
| 10 | + - [Publish Config](#publish-config) |
| 11 | +- [Usage](#usage) |
| 12 | + - [Instantiate Shortener](#instantiate-shortener) |
| 13 | + - [Changing driver](#changing-driver) |
| 14 | +- [License](#license) |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +A Laravel package that is used to shorten URLs according to your needs using your desired URL shortening drivers. |
| 19 | +Every driver will provide different features and function so do please check their documentation |
| 20 | +and pricing for different usages. |
| 21 | + |
| 22 | +## Installation |
| 23 | + |
| 24 | +### Requirements |
| 25 | +The package has been developed to work with the following versions and minimum requirements: |
| 26 | + |
| 27 | +- PHP 7.2 or higher |
| 28 | +- Laravel 5.5 or higher |
| 29 | + |
| 30 | +### Install Pacakge |
| 31 | +You can install the package via the latest Composer: |
| 32 | + |
| 33 | +```bash |
| 34 | +composer require codeofdigital/laravel-url-shortener |
| 35 | +``` |
| 36 | + |
| 37 | +### Publish Config |
| 38 | +You can then publish the package's config file by using the following command: |
| 39 | + |
| 40 | +```bash |
| 41 | +php artisan vendor:publish --provider="CodeOfDigital\LaravelUrlShortener\UrlShortenerServiceProvider" |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +## Usage |
| 46 | +The quickest way to get started with creating a shortened URL is by using the snippet below. |
| 47 | +The ``` shorten() ``` method will return a shortened URL link, and you can freely use it within your system. |
| 48 | +```php |
| 49 | +$shortUrl = new UrlShortener(); |
| 50 | +$shortUrl->shorten('https://example.com'); |
| 51 | +``` |
| 52 | + |
| 53 | +### Instantiate Shortener |
| 54 | +The URL Shortener can be retrieved from the container in few ways: |
| 55 | +```php |
| 56 | +$shortener = app('url.shortener'); |
| 57 | +// or ... |
| 58 | +$shortener = url()->shortener(); |
| 59 | +``` |
| 60 | + |
| 61 | +This package also comes with the URL Shortener facade to instantiate the class: |
| 62 | +```php |
| 63 | +use CodeOfDigital\LaravelUrlShortener\Facades\UrlShortener; |
| 64 | + |
| 65 | +$shortUrl = UrlShortener::shorten('https://example.com'); |
| 66 | +``` |
| 67 | + |
| 68 | +You can also use dependency injection to inject in one of your controller's method: |
| 69 | +```php |
| 70 | +use CodeOfDigital\LaravelUrlShortener\UrlShortener; |
| 71 | + |
| 72 | +class MyController extends Controller |
| 73 | +{ |
| 74 | + public function myFunction(UrlShortener $shortener) |
| 75 | + { |
| 76 | + $shortener->shorten('https://example.com'); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Once you have instantiate the URL Shortener class, you can use the methods and shorten your URLs: |
| 82 | +```php |
| 83 | +// This will set and create the driver instance |
| 84 | +$shortener->driver('own-driver'); |
| 85 | + |
| 86 | +// This will return shortened URL in string |
| 87 | +$shortener->shorten('https://example.com'); |
| 88 | + |
| 89 | +// This will return a promise object which can be used to resolve and retrieve shortened URL later on |
| 90 | +$shortener->shortenAsync('https://example.com'); |
| 91 | + |
| 92 | +// Methods can be called from Laravel URL components |
| 93 | +url()->shorten('https://example.com'); |
| 94 | + |
| 95 | +// Or |
| 96 | +app('url.shortener')->shorten('https://example.com'); |
| 97 | + |
| 98 | +// Methods can be chained as well |
| 99 | +$shortener->driver('own-driver')->shorten('https://example.com'); |
| 100 | +``` |
| 101 | + |
| 102 | +The URL Shortener provides the following methods to use: |
| 103 | + |
| 104 | +Method | Description |
| 105 | +----------------|--------------------------------- |
| 106 | +`shorten` | Shorten the given URL |
| 107 | +`shortenAsync` | Shorten the given URL asynchronously |
| 108 | +`driver` | Set the driver and create the driver instance |
| 109 | + |
| 110 | +### Changing Driver |
| 111 | +You can change the default driver by setting `URL_SHORTENER_DRIVER={driver}` in your environment file |
| 112 | +or publish the config file and make your changes there directly. |
| 113 | + |
| 114 | +## License |
| 115 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
0 commit comments