Download the PHP package mtymek/mini-url without Composer
On this page you can find all versions of the php package mtymek/mini-url. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mtymek/mini-url
More information about mtymek/mini-url
Files in mtymek/mini-url
Package mini-url
Short Description Short URL library, built on PSR-7 and middleware
License 2-Clause BSD
Informations about the package mini-url
MiniUrl - PSR-7 link minifier
Simple URL shortener written in PHP, using PSR-7 & middleware.
It can be used as a free, open-source replacement for bit.ly's core functionality: creating short links and redirecting users.
Installation
Install this library using composer:
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:
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
Expand
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
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:
ShortenApiMiddleware
ShortenApiMiddleware
provides implementation of API for shortening links.
Example usage:
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:
Typically, you will want to wrap ShortenApi
into another middleware that authorizes your users.
ExpandApiMiddleware
ExpandApiMiddleware
provides implementation of API for expanding short links.
Example usage:
You can test it using PHP built-in HTTP server:
$ cd path-to-api
$ php -S localhost:9090 expand-api.php
Expand short link:
See examples
directory for working code.
Repositories
MiniUrl can be plugged into existing applications using RepositoryInterface
, that handles storing and
retrieving ShortUrl
objects:
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.
Repository will assume that it can access short_urls
table, with following structure:
You can create an empty SQLite database using schema file: