PHP code example of afurgeri / laravel-short-links

1. Go to this page and download the library: Download afurgeri/laravel-short-links 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/ */

    

afurgeri / laravel-short-links example snippets


use Modules\ShortLinks\ShortLinks;

$links = app(ShortLinks::class);

$link = $links->create('https://example.com/producto');

$codigo = $link->code;
$urlCorta = $link->url;

$urlCorta = $links->shorten('https://example.com/producto');

$link = $links->create(
    url: 'https://example.com/producto',
    campaignId: '123',
    metadata: [
        'placement' => 'header',
        'source' => 'SistuEmailCampaigns',
    ],
    expiresAt: now()->addDays(30),
);

$accesosCampania = $links->visitsForCampaign('123');
$accesosEnlace = $links->visitsForCode($link->code);

namespace App\Listeners;

use App\Models\Campaign;
use Illuminate\Contracts\Queue\ShouldQueue;
use Modules\ShortLinks\Events\LinkVisited;

final class IncrementCampaignClicks implements ShouldQueue
{
    public function handle(LinkVisited $event): void
    {
        if ($event->campaignId === null) {
            return;
        }

        Campaign::query()
            ->whereKey($event->campaignId)
            ->increment('clicks_count');
    }
}

use App\Listeners\IncrementCampaignClicks;
use Illuminate\Support\Facades\Event;
use Modules\ShortLinks\Events\LinkVisited;

Event::listen(LinkVisited::class, IncrementCampaignClicks::class);

return [
    'storage' => [
        'driver' => env('SHORT_LINKS_STORAGE', 'sql'),
        'connection' => env('SHORT_LINKS_CONNECTION'),
    ],
    'base_url' => env('SHORT_LINKS_BASE_URL'),
    'prefix' => 'r',
    'allowed_hosts' => [],
    'tracking' => [
        'enabled' => true,
        'store_ip' => true,
        'retention_days' => 90,
    ],
    'admin' => [
        'enabled' => false,
        'middleware' => ['web', 'auth', 'verified'],
    ],
];
bash
php artisan vendor:publish --tag=short-links-config --no-interaction
bash
php artisan migrate --no-interaction
bash
php artisan short-links:prune --no-interaction
bash
   php artisan crud:install --no-interaction
   
bash
php artisan db:seed --class='Modules\ShortLinks\Database\Seeders\ShortLinkPermissionSeeder' --no-interaction
php artisan db:seed --class='Modules\ShortLinks\Database\Seeders\LinkVisitPermissionSeeder' --no-interaction