定制 cleaniquecoders/shrinkr 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cleaniquecoders/shrinkr

Composer 安装命令:

composer require cleaniquecoders/shrinkr

包简介

Shrinkr is a Laravel package for shortening URLs, with custom slugs, analytics, branded domains, and seamless API integration.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Shrinkr

Shrinkr is a powerful Laravel package for shortening URLs with custom slugs, comprehensive analytics, health monitoring, and seamless integration.

Transform long URLs into short, shareable links with features like custom slugs, click tracking, branded domains, expiry management, and health monitoring.

Features

URL Shortening - Create short URLs with auto-generated or custom slugs ✅ Analytics Tracking - Track clicks, referrers, IP addresses, browsers, and devices ✅ Branded Domains - Use custom domains for your shortened URLs ✅ Expiry Management - Set expiration times for temporary links ✅ Health Monitoring - Automatically check if original URLs are still reachable ✅ Event System - React to URL access and expiry events ✅ Flexible Logging - Log to files or database ✅ Rate Limiting - Built-in middleware to prevent abuse ✅ Artisan Commands - Automate maintenance tasks

Quick Start

Installation

composer require cleaniquecoders/shrinkr
php artisan vendor:publish --tag="shrinkr-migrations"
php artisan migrate
php artisan vendor:publish --tag="shrinkr-config"

Basic Usage

use CleaniqueCoders\Shrinkr\Facades\Shrinkr;

// Shorten a URL
$shortUrl = Shrinkr::shorten('https://example.com/long-url', auth()->id());
// Result: https://yourdomain.com/s/abc123

// With custom slug
$shortUrl = Shrinkr::shorten('https://example.com', auth()->id(), [
    'custom_slug' => 'my-link',
]);
// Result: https://yourdomain.com/s/my-link

// With expiry (60 minutes)
$shortUrl = Shrinkr::shorten('https://example.com', auth()->id(), [
    'expiry_duration' => 60,
]);

// Resolve short URL to original
$originalUrl = Shrinkr::resolve('abc123');

Documentation

For comprehensive documentation, see the docs directory:

Key Documentation Pages

Usage

Here’s a basic usage example using the Shrinkr facade, actions, and events.

1. Shorten a URL

You can shorten a URL with or without a custom slug and expiry duration.

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

2. Retrieve the Original URL

Use the resolve() method to retrieve the original URL from a shortened one.

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

When the URL is accessed, the UrlAccessed event will be dispatched automatically to track the visit.

3. Update an Existing Short URL

You can update an existing short URL with a new custom slug or expiry duration.

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

4. Event Handling

UrlAccessed Event

The UrlAccessed event is dispatched whenever a shortened URL is accessed. You can listen for this event to log analytics or trigger notifications.

Example: Log URL Access in a Listener

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(),
        ]);
    }
}

UrlExpired Event

The UrlExpired event is dispatched when a URL has expired, either through a scheduled check or upon access. You can listen to this event to notify the user or perform other actions.

Example: Notify on URL Expiry in a Listener

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.
    }
}

5. Automatically Expire URLs

If you’ve set an expiry duration, the URL will be marked as expired once that time has passed. You can also run the expiry command manually or schedule it.

Run the Expiry Command Manually:

php artisan shrinkr:check-expiry

Schedule the Expiry Command:

In your app/Console/Kernel.php:

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

6. Monitor URL Health

The Link Health Monitoring feature allows you to check if URLs are reachable and mark them as active or expired.

Check Health Action

Use the CheckUrlHealthAction to manually check the health of a specific URL.

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.";
}

Check Health Command

Use the Artisan command to check the health of all URLs in bulk.

php artisan shrinkr:check-health

This command will:

  1. Check the status of all URLs.
  2. Mark expired URLs and dispatch the UrlExpired event.
  3. Provide real-time output on the status of each URL.

Example output:

URL abc123 is now marked as active.
URL xyz456 is now marked as expired.
URL health check completed.

Schedule the Health Check Command

You can automatically run the health check at regular intervals using Laravel’s scheduler.

In your app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    $schedule->command('shrinkr:check-health')->hourly();
}

This will ensure that all URLs are continuously monitored and marked as expired when necessary.

7. Redirect Tracking

The redirect feature tracks detailed information such as:

  • IP address of the visitor
  • Browser and OS (via User-Agent parsing)
  • Referrer (where the link was clicked)
  • Headers and query parameters
  • Optionally store logs in a database or log file

Example database log entry:

url_id ip browser platform referrer created_at
1 192.168.1.1 Chrome Windows google.com 2024-10-18 12:34:56

8. Routing

You can configure custom domain for the URL by configuring:

'domain' => 'bite.ly',

You may also change the middleware or add new one by configuring:

'middleware' => ['auth', 'verified', 'throttle:600,1']

Testing

Run the tests using:

composer test

Changelog

Refer to the CHANGELOG for the latest updates and changes.

Contributing

We welcome contributions! Please see CONTRIBUTING for guidelines.

Security Vulnerabilities

Report security vulnerabilities by reviewing our security policy.

Credits

License

Shrinkr is open-sourced software licensed under the MIT license.

cleaniquecoders/shrinkr 适用场景与选型建议

cleaniquecoders/shrinkr 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.28k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2024 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Cleanique Coders」 「shrinkr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cleaniquecoders/shrinkr 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cleaniquecoders/shrinkr 我们能提供哪些服务?
定制开发 / 二次开发

基于 cleaniquecoders/shrinkr 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-19