URL-Shortener
, (*1)
- About
- Install
- Use
- Testing
About URL-Shortener
URL-Shortener is a package for creating short url links that track conversions for Laravel apps or websites., (*2)
Just like bit.ly, you can create a url shortening service in your Laravel app., (*3)
Easy to install and easy to use., (*4)
Just read this file and let me know if I can help., (*5)
Install URL-Shortener
Add this to the composer.json
file:, (*6)
{
"require": {
"serbanblebea/urlshortener": "1.0"
}
}
Or just use the command line:, (*7)
composer require serbanblebea/urlshortener
Then add the Service Provider and the Facade to the config/app.php file, (*8)
UrlShortenerServiceProvider
'providers' => [
SerbanBlebea\UrlShortener\UrlShortenerServiceProvider::class,
];
ShortUrl Facade
'aliases' => [
'ShortUrl' => SerbanBlebea\UrlShortener\Facades\ShortUrl::class,
];
Use URL-Shortener
URL-Shortener is very easy to use:, (*9)
Step 1. Create the table
Before using the package, use the command line php artisan migrate
to migrate the database table links
., (*10)
This will be used to store the data for the short urls and the visitor count for every link., (*11)
Step 2. Publish the config file
Run php artisan vendor:publish
and select the package name to publish the config file url-shortener.php
in the config
folder., (*12)
!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, (*13)
Step 3. Create your first short url
After you migrated the table, it's time to create your first short url:, (*14)
<?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
}
}
Step 4. Change the unique id
Every short url has an unique id that is used for accessing the destination link., (*15)
Exemple: http://your-domain.com/s/unique-id
, (*16)
Usually the unique id is string composed from 8 characters, so you may want to personalize it., (*17)
You can do that with:, (*18)
<?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
, (*19)
Step 4. Count clicks of add Google UTM taggs
You can track clicks to your url directly from your app, or you can use Google Analytics., (*20)
Let's first look at how you can get the number of clicks from the database:, (*21)
<?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., (*22)
Add tracking when you create the short url:, (*23)
<?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:, (*24)
<?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'
]);
}
}
Step 5. Reset the tracking counter
There is a method to reset the tracking counter. Very easy to use., (*25)
<?php
namespace App\Http\Controllers;
use ShortUrl;
class TestController extends Controller
{
public function resetCount()
{
$count = ShortUrl::get('name-of-the-short-url')->resetCounter();
}
}
Step 6. Special Commands
url:make
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., (*26)
Just type php artisan url:make <just-url-after-app-root> <name-of-the-url>
, (*27)
For example: php artisan url:make /test/page/1 FirstBlogPost
. This will create a short url with the name FirstBlogPost
., (*28)
The name of the url can be nulled in the CLI, this will generate a random string that you can change later, (*29)
url:print
You can print all your short url's with one command line php artisan url:print
, (*30)
If you want to search url by name, just add php artisan url:print --name=<name-of-url-here>
, (*31)
Also you can search by destination with php artisan url:print --dest=<name-of-destination-url>
, (*32)
Testing