PHP code example of yorcreative / laravel-urlshortener
1. Go to this page and download the library: Download yorcreative/laravel-urlshortener 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/ */
yorcreative / laravel-urlshortener example snippets
/**
* Find a Short URL by its identifier
*/
$shortUrl = UrlService::findByIdentifier('identifier');
// returns instance of ShortUrl Model.
/**
* Find a Short URL by its hashed signature
*/
$shortUrl = UrlService::findByHash(md5('long_url'));
// returns instance of ShortUrl Model.
/**
* Find a Short URL by its plain text long url string
*/
$shortUrl = UrlService::findByPlainText('long_url');
// returns instance of ShortUrl Model.
/**
* Find or Create - returns existing ShortUrl if found, or UrlBuilder for new creation
* This is useful when you want to avoid exceptions for duplicate URLs
*/
$result = UrlService::findOrCreate('long_url');
// returns ShortUrl if exists, or UrlBuilder if new
// Usage example:
$result = UrlService::findOrCreate('https://example.com/my-long-url');
if ($result instanceof ShortUrl) {
// URL already exists, use existing short URL
$shortUrl = $result;
} else {
// New URL, continue building with options
$shortUrl = $result->withExpiration(Carbon::now()->addWeek()->timestamp)->build();
}
/**
* Find shortUrls by UTM combinations.
*
* Note* This method only accepts the following array fields:
* - utm_id
* - utm_campaign
* - utm_source
* - utm_medium
* - utm_content
* - utm_term
*/
$shortUrlCollection = UrlService::findByUtmCombination([
'utm_campaign' => 'alpha',
'utm_source' => 'bravo',
'utm_medium' => 'testing'
])
// returns an instance of Eloquent Collection of ShortUrl Models.
/**
* Soft delete a Short URL by identifier
*/
UrlService::delete('identifier');
// With multi-domain support
UrlService::delete('identifier', 'short.io');
/**
* Restore a soft-deleted Short URL
*/
UrlService::restore('identifier');
// With multi-domain support
UrlService::restore('identifier', 'short.io');
/**
* Get the successfully routed clicks for all active short urls that are owned by Model IDs 1,2,3 and 4.
* Set the offset of results by 1500 clicks and limit by the results by 500.
*/
$clicks = ClickService::get([
'ownership' => Model::whereIn('id', [1,2,3,4])->get()->toArray(),
'outcome' => [
1, // successful_routed
],
'status' => [
'active',
],
'utm_campaign' => [
'awareness',
],
'utm_source' => [
'github',
],
'limit' => 500,
'offset' => 1500,
]);
// EventServiceProvider or listener registration
use YorCreative\UrlShortener\Events\ShortUrlCreated;
use YorCreative\UrlShortener\Events\ShortUrlClicked;
use YorCreative\UrlShortener\Events\ShortUrlExpired;
// Example listener
Event::listen(ShortUrlCreated::class, function (ShortUrlCreated $event) {
Log::info('Short URL created', [
'identifier' => $event->shortUrl->identifier,
'url' => $event->builtUrl,
]);
});
// Find by identifier on specific domain
$shortUrl = UrlService::findByIdentifier('abc123', 'short.io');
// Find all URLs for a domain
$shortUrls = UrlService::findByDomain('short.io');
// Find or create with domain
$result = UrlService::findOrCreate('https://example.com', 'short.io');
// Both can coexist
UrlService::shorten('https://site-a.com')->forDomain('short.io')->build();
// https://short.io/s/abc123 -> https://site-a.com
UrlService::shorten('https://site-b.com')->forDomain('link.co')->build();
// https://link.co/abc123 -> https://site-b.com