- About
- Install
- Use
- Testing
URL-Shortener is a package for creating short url links that track conversions for Laravel apps or websites.
Just like bit.ly, you can create a url shortening service in your Laravel app.
Easy to install and easy to use.
Just read this file and let me know if I can help.
Add this to the composer.json
file:
{
"require": {
"serbanblebea/urlshortener": "1.0"
}
}
Or just use the command line:
composer require serbanblebea/urlshortener
Then add the Service Provider and the Facade to the config/app.php file
'providers' => [
SerbanBlebea\UrlShortener\UrlShortenerServiceProvider::class,
];
'aliases' => [
'ShortUrl' => SerbanBlebea\UrlShortener\Facades\ShortUrl::class,
];
URL-Shortener is very easy to use:
Before using the package, use the command line php artisan migrate
to migrate the database table links
.
This will be used to store the data for the short urls and the visitor count for every link.
Run php artisan vendor:publish
and select the package name to publish the config file url-shortener.php
in the config
folder.
!IMPORTANT If you change the special_route_param
, all your existing linksspread across the internet will be nulled, so I would setup this option before using the package in production
After you migrated the table, it's time to create your first short url:
<?php
namespace App\Http\Controllers;
use SerbanBlebea\UrlShortener\ShortUrl;
class TestController extends Controller
{
public function index()
{
$url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com');
$short_url = $url->getShortUrl()
// return http://www.name-of-your-host.com/s/fs53rw7h
// 's' => name of the 'special_route_param' in config file
}
}
Every short url has an unique id that is used for accessing the destination link.
Exemple: http://your-domain.com/s/unique-id
Usually the unique id is string composed from 8 characters, so you may want to personalize it.
You can do that with:
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function index()
{
// old_id = 'eujfg849'
// new_id = 'soda'
// Use this method to change the unique id
ShortUrl::changeUniqueId('eujfg849', 'soda');
}
}
This will be your new short url: http://your-domain.com/s/soda
You can track clicks to your url directly from your app, or you can use Google Analytics.
Let's first look at how you can get the number of clicks from the database:
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function index()
{
// Get the short url from database by url name
ShortUrl::count('name-of-url');
}
}
If you want something more advanced, let's add some tracking for Google Analytics.
Add tracking when you create the short url:
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function index()
{
$url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com', 'campaign-name', 'medium-name', 'source-name');
}
}
Or add tracking after you created the short url:
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function index()
{
$url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com');
$url->update([
'campaign' => 'campaign-name',
'medium' => 'medium-name',
'source' => 'source-name'
]);
}
}
There is a method to reset the tracking counter. Very easy to use.
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function resetCount()
{
$count = ShortUrl::get('name-of-the-short-url')->resetCounter();
}
}
After you run php artisan vendor:publish
(see above), you will also have access to a special command for creating short url from the CLI.
Just type php artisan url:make <just-url-after-app-root> <name-of-the-url>
For example: php artisan url:make /test/page/1 FirstBlogPost
. This will create a short url with the name FirstBlogPost
.
The name of the url can be nulled in the CLI, this will generate a random string that you can change later
You can print all your short url's with one command line php artisan url:print
If you want to search url by name, just add php artisan url:print --name=<name-of-url-here>
Also you can search by destination with php artisan url:print --dest=<name-of-destination-url>