PHP code example of ashallendesign / short-url

1. Go to this page and download the library: Download ashallendesign/short-url library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ashallendesign / short-url example snippets


use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->make();
$shortURL = $shortURLObject->default_short_url;

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->urlKey('custom-key')->make();
$shortURL = $shortURLObject->default_short_url;

// Short URL: https://webapp.com/short/custom-key

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits(false)->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackIPAddress()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackBrowser()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackBrowserVersion()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystem()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackOperatingSystemVersion()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackDeviceType()->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->trackVisits()->trackRefererURL()->make();

use AshAllenDesign\ShortURL\Models\ShortURL;
use AshAllenDesign\ShortURL\Facades\ShortURL as ShortUrlBuilder;

$tenantId = 123;

$shortURL = ShortUrlBuilder::destinationUrl($url)
    ->beforeCreate(function (ShortURL $model): void {
        $model->tenant_id = $tenantId;
    })
    ->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')->singleUse()->make();
 

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com')->secure()->make();

// Destination URL: https://destination.com
 

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com?param1=test')->forwardQueryParams()->make();
 

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('http://destination.com')->redirectStatusCode(302)->make();

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->activateAt(\Carbon\Carbon::now()->addDay())->make();
 

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->activateAt(\Carbon\Carbon::now()->addDay())
                           ->deactivateAt(\Carbon\Carbon::now()->addDays(2))
                           ->make();
 

use AshAllenDesign\ShortURL\Classes\Builder;

$shortURLObject = app(Builder::class)->destinationUrl('https://destination.com')
    ->generateKeyUsing(12345)
    ->make();
 



namespace App\Http\Controllers;

use ShortURL;

class Controller
{
    public function index()
    {
        $shortURLObject = ShortURL::destinationUrl('https://destination.com')->make();
        ...
    }
}

use AshAllenDesign\ShortURL\Classes\Builder;
 
$builder = app(Builder::class)->destinationUrl('https://destination.com');

if ($request->date('activation')) {
    $builder = $builder->activateAt($request->date('activation'));
};

$shortURLObject = $builder->make();

use AshAllenDesign\ShortURL\Classes\Builder;
use Carbon\Carbon;
 
$shortURLObject = app(Builder::class)
    ->destinationUrl('https://destination.com')
    ->when(
        $request->date('activation'),
        function (Builder $builder, Carbon $activateDate): Builder  {
            return $builder->activateAt($activateDate);
        },
    )
    ->make();
 

Route::get('/custom/{shortURLKey}', '\AshAllenDesign\ShortURL\Controllers\ShortURLController');

'default_url' => 'https://example.com',

\AshAllenDesign\ShortURL\Facades\ShortURL::routes();

'url_key_generator' => \AshAllenDesign\ShortURL\Classes\KeyGenerator::class,

'user_agent_driver' => \AshAllenDesign\ShortURL\Classes\UserAgent\ParserPhpDriver::class,

'allowed_url_schemes' => [
    'http://',
    'https://',
    'mailto://',
    'myapp://',
],

$shortURL = \AshAllenDesign\ShortURL\Models\ShortURL::find(1);
$visits = $shortURL->visits;

$shortURL = \AshAllenDesign\ShortURL\Models\ShortURL::findByKey('abc123');

$shortURLs = \AshAllenDesign\ShortURL\Models\ShortURL::findByDestinationURL('https://destination.com');

$shortURL = \AshAllenDesign\ShortURL\Models\ShortURL::first();
$shortURL->trackingEnabled();

$shortURL = \AshAllenDesign\ShortURL\Models\ShortURL::first();
$shortURL->trackingFields();

use AshAllenDesign\ShortURL\Models\ShortURL;

$shortUrl = ShortURL::factory()->create();

// URL is deactivated
$deactivatedShortUrl = ShortURL::factory()->deactivated()->create();

// URL is neither activated nor deactivated
$inactiveShortURL = ShortURL::factory()->inactive()->create();

'factories' => [
    \AshAllenDesign\ShortURL\Models\ShortURL::class => \AshAllenDesign\ShortURL\Models\Factories\ShortURLFactory::class,
    \AshAllenDesign\ShortURL\Models\ShortURLVisit::class => \AshAllenDesign\ShortURL\Models\Factories\ShortURLVisitFactory::class
],
bash
php artisan vendor:publish --provider="AshAllenDesign\ShortURL\Providers\ShortURLProvider"
bash
php artisan migrate
 web.php 
 short-url.php