PHPackages                             mtymek/mini-url - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. mtymek/mini-url

ActiveLibrary[HTTP &amp; Networking](/categories/http)

mtymek/mini-url
===============

Short URL library, built on PSR-7 and middleware

1.0.0(10y ago)48522[1 PRs](https://github.com/mtymek/MiniUrl/pulls)2-Clause BSDPHP

Since Jun 25Pushed 9y ago1 watchersCompare

[ Source](https://github.com/mtymek/MiniUrl)[ Packagist](https://packagist.org/packages/mtymek/mini-url)[ RSS](/packages/mtymek-mini-url/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (5)Used By (0)

MiniUrl - PSR-7 link minifier
=============================

[](#miniurl---psr-7-link-minifier)

**Simple URL shortener written in PHP, using PSR-7 &amp; middleware.**

It can be used as a free, open-source replacement for bit.ly's core functionality: creating short links and redirecting users.

[![Build Status](https://camo.githubusercontent.com/ca0b2d51d5c4d88306088c0f88e235e7f3f0d7be720f7966eccd25b83447d4fa/68747470733a2f2f7472617669732d63692e6f72672f6d74796d656b2f4d696e6955726c2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mtymek/MiniUrl)[![Coverage Status](https://camo.githubusercontent.com/06f9adff3d698b2bc01c66c4ba096c88bee9b2f026a3244954a7f80729dd6f1d/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6d74796d656b2f4d696e6955726c2f62616467652e737667)](https://coveralls.io/r/mtymek/MiniUrl)

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

[](#installation)

Install this library using composer:

```
composer require mtymek/mini-url
```

Usage
-----

[](#usage)

There are many ways of using MiniUrl, depending on your needs. You can incorporate it into your app and use it as a part of your business logic, or you can use provided middleware to create a website that exposes link shortening directly to your users.

Before you start, you need to create new instance of `ShortUrlService`, pass base URL used for link generation (your short domain) and repository that will take care of storing short URLs:

```
$pdo = new PDO("sqlite:links.db");
$service = new ShortUrlService('http://sho.rt', new PdoRepository($pdo));
```

Short URL service
-----------------

[](#short-url-service)

`ShortUrlService` is the foundation of MiniUrl - it is what you want to use when you need to shorten URLs inside your application logic.

### Shorten link

[](#shorten-link)

```
$url = $service->shorten('http://github.com/zendframework/zend-diactoros');
echo $url->getShortUrl();

// example output: http://sho.rt/Wwr3bMu
```

### Expand

[](#expand)

```
$url = $service->expand('http://sho.rt/ho3nf1');
header('Location: ' . $url->getLongUrl());
```

Middleware
----------

[](#middleware)

Typically, URL shortener should expose two functionalities: generating short links and redirecting users to full URLs. `MiniUrl` comes with handy middleware which makes it extremely easy. Based on PSR-7 standard, they can be easily wrapped inside other middleware that handles authentication or routing.

### RedirectMiddleware

[](#redirectmiddleware)

When user opens short link in his browser, he is expected to be redirected to destination URL. This can be easily done using `RedirectMiddleware`. It takes the incoming request, extracts a `path` part from URI (domain and query are ignored), finds matching long URL and finally redirect user. If link cannot be found in the repository, response with 404 code is returned.

Example usage:

```
$redirector = new RedirectMiddleware($service);

$server = Zend\Diactoros\Server::createServer(
    $redirector,
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);
$server->listen();
```

### ShortenApiMiddleware

[](#shortenapimiddleware)

`ShortenApiMiddleware` provides implementation of API for shortening links.

Example usage:

```
$shortenApi = new ShortenApiMiddleware($service);

$server = Zend\Diactoros\Server::createServer(
    $shortenApi,
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);
$server->listen();
```

You can test it using PHP built-in HTTP server:

```
$ cd path-to-api
$ php -S localhost:8080 shorten-api.php

```

Create short link using CURL:

```
$ curl --data "longUrl=http://mateusztymek.pl/lorem-ipsum-dolor/" http://localhost:8080
http://sho.rt/bloq3y
```

Typically, you will want to wrap `ShortenApi` into another middleware that authorizes your users.

### ExpandApiMiddleware

[](#expandapimiddleware)

`ExpandApiMiddleware` provides implementation of API for expanding short links.

Example usage:

```
$expandApi = new ExpandApiMiddleware($service);

$server = Zend\Diactoros\Server::createServer(
    $expandApi,
    $_SERVER,
    $_GET,
    $_POST,
    $_COOKIE,
    $_FILES
);
$server->listen();
```

You can test it using PHP built-in HTTP server:

```
$ cd path-to-api
$ php -S localhost:9090 expand-api.php

```

Expand short link:

```
$ curl --data "shortUrl=http://sho.rt/bloq3y" http://localhost:9090
http://mateusztymek.pl/lorem-ipsum-dolor
```

See `examples` directory for working code.

Repositories
------------

[](#repositories)

MiniUrl can be plugged into existing applications using `RepositoryInterface`, that handles storing and retrieving `ShortUrl` objects:

```
interface RepositoryInterface
{
    public function findByLongUrl($longUrl);
    public function findByShortUrl($shortUrl);
    public function save(ShortUrl $shortUrl);
}
```

### `PdoRepository`

[](#pdorepository)

`PdoRepository` is a universal repository that allows using MiniUrl with any database (queries are very simple...). In order to use it, you have to pass `PDO` database handle to repository constructor.

```
$pdo = new PDO("sqlite:links.db");
$service = new ShortUrlService('http://mini.me', new PdoRepository($pdo));
$short = $service->shorten('http://google.com');
echo $short->getShortUrl();
```

Repository will assume that it can access `short_urls` table, with following structure:

```
CREATE TABLE short_urls (
  long_url VARCHAR(256) PRIMARY KEY NOT NULL,
  short_url VARCHAR(256) NOT NULL,
  creation_date INT NOT NULL
);
CREATE INDEX short_url_idx ON short_urls(short_url);
```

You can create an empty SQLite database using schema file:

```
$ sqlite3 links.db
