MremiUrlShortenerBundle
, (*1)
, (*2)
This bundle implements the UrlShortener library for Symfony., (*3)
License
This bundle is available under the MIT license., (*4)
Prerequisites
This version of the bundle requires Symfony 2.8, 3.0 or newer., (*5)
For compatibility with Symfony 2.7 or earlier, please use 1.0.* versions of
this bundle., (*6)
Basic Docs, (*7)
, (*8)
Installation
Installation is a quick 5 step process:, (*9)
- Download MremiUrlShortenerBundle using composer
- Enable the Bundle
- Create your Link class (optional)
- Configure the MremiUrlShortenerBundle
- Update your database schema (optional)
Step 1: Download MremiUrlShortenerBundle using composer
Require mremi/url-shortener-bundle
via composer:, (*10)
php composer.phar require mremi/url-shortener-bundle
Note: if you are using Symfony 2.7 or earlier, please require ~1.0.0 version:, (*11)
php composer.phar require mremi/url-shortener-bundle:~1.0.0
Composer will modify your composer.json file and install the bundle to your
project's vendor/mremi
directory., (*12)
Step 2: Enable the bundle
Enable the bundle in the kernel:, (*13)
``` php
For now, only Doctrine ORM is handled by this bundle (any PR will be
> appreciated :) ).
``` php
, (*14)
<entity name="Acme\UrlShortenerBundle\Entity\Link"
table="link">
<id name="id" column="id" type="integer">
<generator strategy="AUTO" />
</id>
</entity>
, (*15)
``` php
addSql("CREATE INDEX idx_url_shortener_link_long_url ON link (long_url(20))");
}
public function down(Schema $schema)
{
$this->addSql("DROP INDEX idx_url_shortener_link_long_url ON link;");
}
```
### Step 4: Configure the MremiUrlShortenerBundle
Fow now, you just have to configure your Bit.ly username and password.
```yaml
# app/config/config.yml
mremi_url_shortener:
link_class: Mremi\UrlShortener\Model\Link
providers:
bitly:
enabled: true
username: your_bitly_username
password: your_bitly_password
options:
connect_timeout: 1
timeout: 1
google:
enabled: true
api_key: your_api_key
options:
connect_timeout: 1
timeout: 1
```
### Step 5: Update your database schema (optional)
If you configured the data storage (step 3), you can now update your database
schema.
If you want to first see the create table query:
``` bash
$ app/console doctrine:schema:update --dump-sql
```
Then you can run it:
``` bash
$ app/console doctrine:schema:update --force
```
## Chain providers
One service allow you to shorten/expand URL, to use like this:
```php
get('mremi_url_shortener.link_manager');
$chainProvider = $container->get('mremi_url_shortener.chain_provider');
$link = $linkManager->create();
$link->setLongUrl('http://www.google.com');
$chainProvider->getProvider('bitly')->shorten($link);
$chainProvider->getProvider('google')->expand($link);
```
## Custom provider
You can add your own provider to the chain providers:
1. Create a service which implements `\Mremi\UrlShortener\Provider\UrlShortenerProviderInterface`
2. Add the tag `mremi_url_shortener.provider`
``` xml
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="acme.custom_provider" class="Acme\YourBundle\Provider\CustomProvider">
<tag name="mremi_url_shortener.provider" />
</service>
</services>
</container>
, (*16)
You can now test the providers you configured with the following command line:, (*17)
``` bash
$ app/console mremi:url-shortener:test, (*18)
<a name="retrieve-link"></a>
## Retrieve link
You can retrieve some links using these finders:
```php
<?php
$linkManager = $container->get('mremi_url_shortener.link_manager');
$shortened = $linkManager->findOneByProviderAndShortUrl('bitly', 'http://bit.ly/ze6poY');
$expanded = $linkManager->findOneByProviderAndLongUrl('google', 'http://www.google.com');
If you configured the data storage (steps 3 & 5), finders look first in
database ; if the link exists then return it, otherwise an API call will be
done and link will be saved., (*19)
Else this will consume an API call., (*20)
, (*21)
Twig functions
You can also simply shorten/expand a URL from a twig file. It should be used
with caution if no data storage is configured, because it's not HTTP friendly., (*22)
``` html+jinja
{# src/Acme/YourBundle/Resources/views/index.html.twig #}, (*23)
{{ mremi_url_shorten('bitly', 'http://www.google.com') }}
{{ mremi_url_expand('google', 'http://goo.gl/fbsS') }}
```
, (*24)
Profiler
If your are in debug mode (see your front controller), you can check in the web
debug toolbar the configured providers and some statistics from the current
HTTP request: number of requests per provider, consumed memory, request
duration..., (*25)
, (*26)
, (*27)
Contribution
Any question or feedback? Open an issue and I will try to reply quickly., (*28)
A feature is missing here? Feel free to create a pull request to solve it!, (*29)
I hope this has been useful and has helped you. If so, share it and recommend
it! :), (*30)
@mremitsme, (*31)