PHP code example of cleaniquecoders / shrinkr

1. Go to this page and download the library: Download cleaniquecoders/shrinkr 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/ */

    

cleaniquecoders / shrinkr example snippets


return [
    'logger' => \CleaniqueCoders\Shrinkr\Actions\Logger\LogToFile::class, // Default logger
];

'logger' => \CleaniqueCoders\Shrinkr\Actions\Logger\LogToDatabase::class,

use CleaniqueCoders\Shrinkr\Facades\Shrinkr;

// Shorten a URL with default random slug
$shortUrl = Shrinkr::shorten('https://example.com/long-url', auth()->id());
echo $shortUrl; // Outputs: https://yourdomain.com/abc123

// Shorten a URL with a custom slug and expiry duration (e.g., 60 minutes)
$shortUrlWithCustomSlug = Shrinkr::shorten('https://example.com/long-url', auth()->id(), [
    'custom_slug' => 'my-slug',
    'expiry_duration' => 60, // Expires in 60 minutes
]);
echo $shortUrlWithCustomSlug; // Outputs: https://yourdomain.com/my-slug

$originalUrl = Shrinkr::resolve('abc123');
echo $originalUrl; // Outputs: https://example.com/long-url

use CleaniqueCoders\Shrinkr\Models\Url;
use CleaniqueCoders\Shrinkr\Facades\Shrinkr;

$url = Url::find(1);

// Update the short URL with a new slug and expiry duration
$updatedUrl = Shrinkr::update($url, [
    'custom_slug' => 'updated-slug',
    'expiry_duration' => 120, // 2 hours from now
]);
echo $updatedUrl->shortened_url; // Outputs: https://yourdomain.com/updated-slug

namespace CleaniqueCoders\Shrinkr\Listeners;

use CleaniqueCoders\Shrinkr\Events\UrlAccessed;
use Illuminate\Support\Facades\Log;

class LogUrlAccess
{
    public function handle(UrlAccessed $event)
    {
        $url = $event->url;

        Log::info('URL accessed', [
            'url_id' => $url->id,
            'shortened_url' => $url->shortened_url,
            'accessed_at' => now(),
        ]);
    }
}

namespace CleaniqueCoders\Shrinkr\Listeners;

use CleaniqueCoders\Shrinkr\Events\UrlExpired;
use Illuminate\Support\Facades\Log;

class NotifyUrlExpired
{
    public function handle(UrlExpired $event)
    {
        $url = $event->url;

        Log::warning('URL expired', [
            'url_id' => $url->id,
            'shortened_url' => $url->shortened_url,
            'expired_at' => now(),
        ]);

        // Optionally, notify the user about the expired URL.
    }
}

$schedule->command('shrinkr:check-expiry')->hourly();

use CleaniqueCoders\Shrinkr\Actions\CheckUrlHealthAction;
use CleaniqueCoders\Shrinkr\Models\Url;

$url = Url::find(1); // Retrieve URL instance

$action = new CheckUrlHealthAction();
$isHealthy = $action->execute($url);

if ($isHealthy) {
    echo "URL is active.";
} else {
    echo "URL is expired.";
}

protected function schedule(Schedule $schedule)
{
    $schedule->command('shrinkr:check-health')->hourly();
}
bash
php artisan vendor:publish --tag="shrinkr-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="shrinkr-config"
bash
php artisan shrinkr:check-expiry
bash
php artisan shrinkr:check-health