|
| 1 | +# laravel-url-shortener |
| 2 | +Powerful URL shortening tools in Laravel |
| 3 | + |
| 4 | +<p align="center"> |
| 5 | + <a href="https://travis-ci.org/LaraCrafts/laravel-url-shortener"><img src="https://travis-ci.org/LaraCrafts/laravel-url-shortener.svg?branch=master"></a> |
| 6 | + <a href="https://packagist.org/packages/laracrafts/laravel-url-shortenern"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/downloads"></a> |
| 7 | + <a href="https://packagist.org/packages/laracrafts/laravel-url-shortener"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/version"></a> |
| 8 | + <a href="https://scrutinizer-ci.com/g/LaraCrafts/laravel-url-shortener/"><img src="https://scrutinizer-ci.com/g/LaraCrafts/laravel-url-shortener/badges/coverage.png?b=master"></a> |
| 9 | + <a href="https://packagist.org/packages/laracrafts/laravel-url-shortener"><img src="https://poser.pugx.org/laracrafts/laravel-url-shortener/license"></a> |
| 10 | +</p> |
| 11 | + |
| 12 | +- [Installation](#installation) |
| 13 | + - [Requirements](#requirements) |
| 14 | + - [Laravel 5.5+](#laravel-55) |
| 15 | + - [Laravel 5.1-5.4](#laravel-51-54) |
| 16 | + - [Lumen](#lumen) |
| 17 | +- [Usage](#usage) |
| 18 | + - [Changing the default driver](#changing-the-default-driver) |
| 19 | +- [Available drivers](#available-drivers) |
| 20 | + - [Bit.ly](#bitly) |
| 21 | + - [Shorte.st](#shortest) |
| 22 | + - [TinyURL](#tinyurl) |
| 23 | + |
| 24 | +## Installation |
| 25 | +You can easily install this package using Composer, by running the following command: |
| 26 | + |
| 27 | +```bash |
| 28 | +composer require laracrafts/laravel-url-shortener |
| 29 | +``` |
| 30 | + |
| 31 | +### Requirements |
| 32 | +This package has the following requirements: |
| 33 | + |
| 34 | +- PHP 7.1 or higher |
| 35 | +- Laravel (or Lumen) 5.1 or higher |
| 36 | + |
| 37 | +### Laravel 5.5+ |
| 38 | +If you use Laravel 5.5 or higher, that's it. You can now use the package, continue to the [usage](#usage) section. |
| 39 | + |
| 40 | +### Laravel 5.1-5.4 |
| 41 | +If you're using an older version of Laravel, register the package's service provider to your application. You can do |
| 42 | +this by adding the following line to your `config/app.php` file: |
| 43 | + |
| 44 | +```php |
| 45 | +'providers' => [ |
| 46 | + ... |
| 47 | + LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class, |
| 48 | + ... |
| 49 | +], |
| 50 | +``` |
| 51 | + |
| 52 | +### Lumen |
| 53 | +If you're using Lumen, register the package's service provider by adding the following line to your `bootstrap/app.php` |
| 54 | +file: |
| 55 | + |
| 56 | +```php |
| 57 | +$app->register(LaraCrafts\UrlShortener\UrlShortenerServiceProvider::class); |
| 58 | +``` |
| 59 | + |
| 60 | +## Usage |
| 61 | +The shortener can be retrieved from the container in two ways: |
| 62 | + |
| 63 | +```php |
| 64 | +// This works in both Laravel and Lumen |
| 65 | +$shortener = app('url.shortener'); |
| 66 | +// This only works in Laravel 5.2+ |
| 67 | +$shortener = url()->shortener(); |
| 68 | +``` |
| 69 | + |
| 70 | +Once you have an instance of the shortener, you can shorten your URLs: |
| 71 | + |
| 72 | +```php |
| 73 | +// This will return your shortened URL as a string |
| 74 | +$shortener->shorten(...); |
| 75 | + |
| 76 | +// This will return a promise which will resolve to your shortened URL |
| 77 | +$shortener->shortenAsync(...); |
| 78 | + |
| 79 | +// You can also call shortening from Laravel's url component directly |
| 80 | +app('url')->shorten(...); |
| 81 | + |
| 82 | +// or... |
| 83 | +url()->shorten(...); |
| 84 | +``` |
| 85 | + |
| 86 | +This package relies on Guzzle's promise library for its asynchronous shortening, read their |
| 87 | +[documentation](https://github.com/guzzle/promises) for more information. |
| 88 | + |
| 89 | +You can also use dependency injection to inject the shortener into a method: |
| 90 | + |
| 91 | +```php |
| 92 | +class MyController extends Controller |
| 93 | +{ |
| 94 | + public function myFunction(ShortenerManager $shortener) |
| 95 | + { |
| 96 | + $shortener->shorten(...); |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +The shortener exposes the following methods: |
| 102 | + |
| 103 | +Method | Description |
| 104 | +---------------|------------------------------------- |
| 105 | +`shorten` | Shorten the given URL |
| 106 | +`shortenAsync` | Shorten the given URL asynchronously |
| 107 | +`driver` | Retrieve a driver (e.g. `tiny_url`) |
| 108 | + |
| 109 | +### Changing the default driver |
| 110 | +You can change the default driver by setting `URL_SHORTENER_DRIVER={driver}` in your environment file or publishing the |
| 111 | +config file and changing it directly. |
| 112 | + |
| 113 | +## Available drivers |
| 114 | +Below is a list of available drivers along with their individual specs: |
| 115 | + |
| 116 | +Service | Driver name | Since version | Analytics | Monetization |
| 117 | +-----------------------|-------------|---------------|-----------|----------------- |
| 118 | +[Bit.ly](#bitly) | `bit_ly` | 1.0.0 | yes | no |
| 119 | +[Shorte.st](#shortest) | `shorte_st` | 1.0.0 | yes | yes |
| 120 | +[TinyURL](#tinyurl) | `tiny_url` | 1.0.0 | no | no |
| 121 | + |
| 122 | +### Bit.ly |
| 123 | +[website](https://bit.ly) |
| 124 | + |
| 125 | +This driver uses the Bit.ly API version 4, which requires an access token. Currently only _generic access tokens_ are |
| 126 | +supported, which you can set using the `BIT_LY_API_TOKEN` environment variable. This driver also supports custom domains |
| 127 | +(if you have a paid Bit.ly account) which can be set through the `BIT_LY_DOMAIN` environment variable. |
| 128 | + |
| 129 | +### Shorte.st |
| 130 | +[website](https://shorte.st) |
| 131 | + |
| 132 | +This driver uses the Shorte.st API, which requires an access token. You can set this token by adding the |
| 133 | +`SHORTE_ST_API_TOKEN` environment variable or publishing and editing the package config file. |
| 134 | + |
| 135 | +### TinyURL |
| 136 | +[website](http://tinyurl.com) |
| 137 | + |
| 138 | +This driver uses the TinyURL API and does not require any additional setup, this driver is also the package default. |
0 commit comments