Shorten URL's using your own domain
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
Laravel 5.8+ (Not tested in previous versions)
- Download it into your Laravel app using composer
composer require cedaesca/urlshortener
- Publish the packages files
php artisan vendor:publish
- Run the migrations.
php artisan migrate
-
Go to the config file located on
config/cedaesca/URLShortener.php
-
Change the
length
value for that of your preference.
You have to add the URLShortener facade to your controller:
use cedaesca\URLShortener\Facades\URLShortener;
Then you'll have access to the create
and redirect
methods.
Use the create
static method to shorten a given URL. This method receives the request as argument and returns an instance of the model if was successfully created or false if not:
URLShortener::create($request);
First off, the redirection route expect a shortlink
parameter, make sure to name it like that. Later, you can redirect the user to the expected target by returning Laravel's redirect response and giving the returning value of the target
method as an argument. The target
method also receives the \Illuminate\Http\Request
instance.
Route::get('/r/{code}', 'UrlShortenerController@redirect')->name('rthis');
return redirect(URLShortener::target($request));
You may want to track some statistics with your shortened URL's. At the moment you can track their user agent, their IP Address and the timestamps. You can decide what to do with that info.
To achieve this, call the log method before the target one in your redirect response. Give the ``\Illuminate\Http\Requestas the argument and now you can leave the
target` argument blank.
return redirect(URLShortener::log($request)->target());
If the code given as argument is invalid, the redirect
method will redirect the user to a default route. Change this from the config file.
-
Go to the config file located on
config/cedaesca/URLShortener.php
-
Change the
default_redirect
value for that of your preference.