PHP code example of mtymek / mini-url

1. Go to this page and download the library: Download mtymek/mini-url 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/ */

    

mtymek / mini-url example snippets


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

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

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

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

$redirector = new RedirectMiddleware($service);

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

$shortenApi = new ShortenApiMiddleware($service);

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

$expandApi = new ExpandApiMiddleware($service);

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

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

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