fasaya/url-shortener

适用于Laravel项目的简单URL缩短库。

v1.0.0 2023-09-28 13:35 UTC

README

Latest Version on Packagist Total Downloads

这是一个用于缩短URL和管理缩短URL的包。

安装

您可以通过composer安装此包。

composer require fasaya/url-shortener

您可以使用以下命令发布和运行迁移:

php artisan vendor:publish --tag="url-shortener-migrations"
php artisan migrate

您可以使用以下命令发布配置文件:

php artisan vendor:publish --tag="url-shortener-config"

这是已发布配置文件的内容。

return [

    /**
     * Here you may specify the fully qualified class name of the user model class.
     */
    'user-model' => App\Models\User::class,

    /**
     * Maximum character length of shortened url slug
     */
    'min-length' => 6,

    /**
     * Maximum character length of shortened url slug
     */
    'max-length' => 10,

    /**
     * URL route for the shortened URL
     * Example
     *  'prefix' => '/l', // https://yourdomain.com/l/exampleString
     */
    'route' => [
        'prefix' => '/l',
        'middleware' => [
            // 'web',
        ],
    ],

    /**
     * Where should the admin route be?
     */
    'admin-route' => [
        'enabled' => true, // Should the admin routes be enabled?
        'as' => 'url-shortener-manager.',
        'prefix' => 'url-shortener-manager',
        'middleware' => [
            'web',
        ],
    ],

    /**
     * Admin Template
     * example
     * 'name' => 'layouts.app' for Default urlShortener use 'url-shortener::layouts.app'
     * 'section' => 'content' for Default urlShortener use 'content'
     * 'styles_section' => 'page_style' for Default urlShortener use 'page_style'
     * 'script_section' => 'page_script' for Default urlShortener use 'page_script'
     */
    'admin-template' => [
        'name' => 'url-shortener::layouts.app',
        'section' => 'content',
        'styles_section' => 'page_style',
        'script_section' => 'page_script',
    ],

    /**
     * Number of emails per page in the admin view
     */
    'links-per-page' => 30,

    /**
     * Date Format
     */
    'date-format' => 'm/d/Y g:i a',

];

可选地,您可以使用以下命令发布URL缩短管理仪表板视图:

php artisan vendor:publish --tag="url-shortener-views"

用法

use Fasaya\UrlShortener\UrlShortener;

// Add an expiration date (optional)
$expire_at = '2025-12-12 00:00:00'; // NULL for no expiration date

// Generate a random short URL
$link = UrlShortener::make('http://example.com', $expire_at)->short_url;

// Generate a custom short URL
$link = UrlShortener::make('http://example.com', 'custom-short-slug', $expire_at)->short_url; 

异常

以下异常可能会抛出。您可以将它们添加到异常处理程序的忽略列表中,或按需处理它们。

  • Fasaya\UrlShortener\Exceptions\UrlNotValidException - 无效的URL。
  • Fasaya\UrlShortener\Exceptions\UrlExistsException - URL已存在且处于活动状态。
  • Fasaya\UrlShortener\Exceptions\TriesLimitReachedException - 生成尝试达到限制。

测试

composer test

变更日志

请参阅变更日志了解最近的变化信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请查阅我们的安全策略了解如何报告安全漏洞。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。