PHPackages                             emrahtoy/url-shortener-and-expander - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. emrahtoy/url-shortener-and-expander

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

emrahtoy/url-shortener-and-expander
===================================

Url shortener and expander library

1.0.3(9y ago)519MITPHPPHP &gt;=5.4.0

Since Apr 23Pushed 9y ago1 watchersCompare

[ Source](https://github.com/emrahtoy/url-shortener-and-expander)[ Packagist](https://packagist.org/packages/emrahtoy/url-shortener-and-expander)[ Docs](https://github.com/emrahtoy/url-shortener-and-expander)[ RSS](/packages/emrahtoy-url-shortener-and-expander/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (0)

PHP URL Shortener and Expander
==============================

[](#php-url-shortener-and-expander)

Url shortener/expander library.

Benefits
--------

[](#benefits)

- Produces short codes depending on the (integer) id of the record
- Supports legacy or custom short urls. So you can move your old system without changing short urls or short codes
- Uses low server resources
- Reports errors in json format via responseJson static function
- Can count redirects ( on Mysql )
- Supports Doctrine Cache Clients ( example includes redis usage via predis )
- Uses alphanumeric characters and supports all browsers
- Uses PDO in order to prevent SQL injection hacks
- Supports URL check before shorten and after expanded ( checks for http 200 code )
- Redirection after expand is optional ( redirects with 301 for SEO compliance )
- You can change allowed characters used in shortened urls code. This may reduce count of possible shortened url code.

Installation
------------

[](#installation)

### Requirements:

[](#requirements)

- PHP
- Mysql DB or Maria DB
- PDO Extension must be enabled
- Credentials will be needed to connect and run SQL queries on database server
- Sql files under sql directory must be applied on database server
- Composer

### Install ( via Composer )

[](#install--via-composer-)

```
$ composer require emrahtoy/url-shortener
```

Using URL shortener service
---------------------------

[](#using-url-shortener-service)

```
$urlShortener = new \UrlCompressor\Shortener($connection, $config);

// returns true or false depending on success and error.
$result = $urlShortener->shorten($url);

// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true"
\UrlCompressor\Common::responseJson($urlShortener->getResult(), false);
```

Using URL expander service
--------------------------

[](#using-url-expander-service)

```
$urlExpander = new \UrlCompressor\Expander($connection, $config);

// returns true or false depending on success and error.
$result = $urlExpander->expand($shortened_code);

// responseJson is a tool to return json with proper headers. This function also redirect with 301 code if you send true as secondary parameter.
// You can send "donotredirect" in order to prevent redirection even it is set "true"
\UrlCompressor\Common::responseJson($urlExpander->getResult(),(isset($_REQUEST['donotredirect']))?false:true);
```

### Available configuration options

[](#available-configuration-options)

```
$config = [
    'CheckUrl' => false, // check url before shortening or after expand
    'ShortUrlTableName' => 'shortenedurls', // Database table name where shortened url codes are stored
    'CustomShortUrlTableName' => 'customshortenedurls', // Database table name where your legacy shortened codes are stored
    'TrackTableName' => 'track', // Database table name where the visit/redirect counts are stored
    'DefaultLocation' => '', // where to redirect if expanded url could not find
    'Track'=>false // Determines if visit/redirects will be stored
];
```

### Using cache

[](#using-cache)

You can use any cache library supported by [Doctrine Cache](https://github.com/doctrine/cache).

This example uses [Predis Client](https://github.com/nrk/predis) with [Redis](https://redis.io)

```
$urlShortener = new \UrlCompressor\Shortener($connection, $config);
$urlExpander = new \UrlCompressor\Expander($connection, $config);

// using Predis Client with Doctrine Cache
try{
    $cacheConfig = [
        'scheme' => 'tcp',
        'host' => '127.0.0.1',
        'port' => 6379,
        'password' => 'supersecretauthentication' // if you have set authentication on redis
    ];

    //lets create redis client
    $cacheClient = new Predis\Client($cacheConfig);
    // and try to connect
    $cacheClient->connect();

    // we can encapsulate cache cilent with doctrine cache if any exception not fired
    $cache = new \Doctrine\Common\Cache\PredisCache($cacheClient);

    // set cache provider for url shortener service
    $urlShortener->setCache($cache);

    // set cache provider for url expander service
    $urlExpander->setCache($cache);

} catch(Exception $e){
    $result=new \UrlCompressor\Result();
    $result->error($e->getMessage());
    \UrlCompressor\Common::responseJson($result->result());
    die();
}
```

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

4

Last Release

3350d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4387637?v=4)[Emrah TOY](/maintainers/emrahtoy)[@emrahtoy](https://github.com/emrahtoy)

---

Top Contributors

[![emrahtoy](https://avatars.githubusercontent.com/u/4387637?v=4)](https://github.com/emrahtoy "emrahtoy (3 commits)")

---

Tags

phpphp-libraryphp7urlbitlyshortenerurl shortenershortenurl shortenexpanderurl expander

### Embed Badge

![Health badge](/badges/emrahtoy-url-shortener-and-expander/health.svg)

```
[![Health](https://phpackages.com/badges/emrahtoy-url-shortener-and-expander/health.svg)](https://phpackages.com/packages/emrahtoy-url-shortener-and-expander)
```

###  Alternatives

[yourls/yourls

Your Own URL Shortener

12.1k27.8k1](/packages/yourls-yourls)[vinelab/url-shortener

URL shortening the easy way.

5917.2k](/packages/vinelab-url-shortener)[laracrafts/laravel-url-shortener

Powerful URL shortening tools in Laravel

97112.0k](/packages/laracrafts-laravel-url-shortener)[waavi/url-shortener

Url Shortener for Laravel 5 with support for Google and Bitly drivers.

64178.1k](/packages/waavi-url-shortener)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
