serbanblebea/urlshortener

这是一个用于为Laravel网站或Web应用创建跟踪转换的短链接的包。

v1.2.1 2018-05-19 14:12 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:34:08 UTC


README

菜单

  • 关于
  • 安装
  • 使用
  • 测试

关于URL-Shortener

URL-Shortener是一个用于创建跟踪Laravel应用或网站转换的短链接的包。

就像bit.ly一样,你可以在Laravel应用中创建一个URL缩短服务。

安装和使用都非常简单。

请阅读此文件,并告诉我我是否能帮到您。

安装URL-Shortener

将以下内容添加到composer.json文件中

{
    "require": {
        "serbanblebea/urlshortener": "1.0"
    }
}

或者直接使用命令行

composer require serbanblebea/urlshortener

然后将服务提供者和外观添加到config/app.php文件中

UrlShortenerServiceProvider

'providers' => [
    SerbanBlebea\UrlShortener\UrlShortenerServiceProvider::class,
];

ShortUrl外观

'aliases' => [
    'ShortUrl' => SerbanBlebea\UrlShortener\Facades\ShortUrl::class,
];

使用URL-Shortener

URL-Shortener使用非常简单

步骤1. 创建表格

在使用此包之前,使用命令行php artisan migrate迁移数据库表links

这将用于存储短链接的数据和每个链接的访问次数。

步骤2. 发布配置文件

运行php artisan vendor:publish并选择包名以发布配置文件url-shortener.phpconfig文件夹。

重要 如果您更改了special_route_param,所有散布在互联网上的现有链接都将被注销,因此我建议在生产环境中使用此包之前设置此选项。

步骤3. 创建第一个短链接

迁移了表之后,是时候创建您的第一个短链接了。

<?php

namespace App\Http\Controllers;

use SerbanBlebea\UrlShortener\ShortUrl;

class TestController extends Controller
{
    public function index()
    {
        $url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com');
        $short_url = $url->getShortUrl()
        // return http://www.name-of-your-host.com/s/fs53rw7h
        // 's' => name of the 'special_route_param' in config file
    }
}

步骤4. 修改唯一ID

每个短链接都有一个唯一的ID,用于访问目标链接。

例如:http://your-domain.com/s/unique-id

通常唯一ID是由8个字符组成的字符串,因此您可能希望对其进行个性化。

您可以通过以下方式做到这一点

<?php

namespace App\Http\Controllers;

use ShortUrl;

class TestController extends Controller
{
    public function index()
    {
        // old_id = 'eujfg849'
        // new_id = 'soda'

        // Use this method to change the unique id
        ShortUrl::changeUniqueId('eujfg849', 'soda');
    }
}

这将成为您的新短链接:http://your-domain.com/s/soda

步骤4. 计数点击并添加Google UTM标签

您可以直接从您的应用中跟踪对URL的点击,或者使用Google Analytics。

让我们首先看看如何从数据库中获取点击次数

<?php

namespace App\Http\Controllers;

use ShortUrl;

class TestController extends Controller
{
    public function index()
    {
        // Get the short url from database by url name
        ShortUrl::count('name-of-url');
    }
}

如果您想更高级一些,让我们为Google Analytics添加一些跟踪。

创建短链接时添加跟踪

<?php

namespace App\Http\Controllers;

use ShortUrl;

class TestController extends Controller
{
    public function index()
    {   
        $url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com', 'campaign-name', 'medium-name', 'source-name');
    }
}

或者在创建短链接后添加跟踪

<?php

namespace App\Http\Controllers;

use ShortUrl;

class TestController extends Controller
{
    public function index()
    {   
        $url = ShortUrl::shortenUrl('name-of-the-url', 'http://url-that-you-want-to-shorten.com');
        $url->update([
            'campaign' => 'campaign-name',
            'medium' => 'medium-name',
            'source' => 'source-name'
        ]);
    }
}

步骤5. 重置跟踪计数器

有一个方法可以重置跟踪计数器。非常容易使用。

<?php

namespace App\Http\Controllers;

use ShortUrl;

class TestController extends Controller
{
    public function resetCount()
    {
        $count = ShortUrl::get('name-of-the-short-url')->resetCounter();
    }
}

步骤6. 特殊命令

url:make

在运行php artisan vendor:publish(见上方)后,您还将获得从CLI创建短链接的特殊命令。

只需键入php artisan url:make <just-url-after-app-root> <name-of-the-url>

例如:php artisan url:make /test/page/1 FirstBlogPost。这将创建一个名为FirstBlogPost的短链接。

在CLI中,您可以为URL名称留空,这将生成一个可以稍后更改的随机字符串

url:print

您可以使用一条命令行php artisan url:print打印出所有短链接。

如果您想按名称搜索URL,只需添加php artisan url:print --name=<name-of-url-here>

您还可以使用php artisan url:print --dest=<name-of-destination-url>按目标搜索

测试