Krishnan's Personal Website


Home | About | Blog | Skills | Innovations | Work with Me | Connect with me | Other Links


Building a URL Shortener with Symfony



Published On: Jul 04 2025
Written By: Krishnan Sethuraman
Category: Programming


building url shortner with symfony

 


I am a big fan of Laravel and I use it for almost everything. However since the past two years I have also been working on the Symfony framework. I had to pick up Symfony for a client and since then I also use it for my projects. 

I create a lot of content especially blog articles and share them online on my social media accounts. While doing so I create an online tool to create a shorter link for my blog articles and then post them online. 

Until recently I have been using random third party url shortner online, but they do not give me a custom domain. The ones that did forced me to make a hefty payment online. 

So I decided to build one with Symfony as a weekend project. I have also open sourced this application on GitHub and you can find the link below. 

Without further adieu let's see how I built this tool. 

Disclaimer: This article doesn't teach you Symfony or Php

Setting up Symfony

I am using Symfony 5.4 because I wanted a version that is compatible with Php 7.4 which is what I am using on a day to day basis. One day I will upgrade my Php version to 8.x.

I am not using the Symfony binary. I am using composer to install Symfony. 

$ composer create-project symfony/skeleton:"^5.4" app

$ cd app

$ composer install

Create a .env.local file for development on localhost. 

$ cp .env .env.local

To ensure permissions do not throw an error. 

$ chmod -R 775 var

Run the following command to ensure there are no issues. 

php bin/console about


To run it locally I used. 

php -S localhost:8000 -t public


Creating Controllers

Maker bundle was already installed. I can use it to create controller etc. It's 

composer require symfony/maker-bundle --dev

The maker bundle will make you run commands like make:controller etc. It's like the artisan command in Laravel. 

Unlike Laravel, Symfony does not have migrations built in. You will have to install the doctrine annotations package for the same.

$ composer require doctrine/annotations 

To ensure everything is fine run, 

$ php bin/console list

Now we can create controllers with the following command. 

$ php bin/console make:controller


Creating Routes

For this application I am using the routes.yaml file as I like keeping the routes in a separate file.

Created my first route by adding the following and the home page opened up. 

home:
    path: /
    controller: App\Controller\HomeController::index


Creating Views with Twig

For the views I am using Twig. I installed the Twig package with the following command. 

$ composer require symfony/twig-bundle

$ composer require twig

Loading a view in Symfony is pretty straight forward. I have shown an example below. 

return $this->render('home/index.html.twig');

 

Connecting with a Database

With the controllers in place I connected the application to a MySQL instance and database. I updated the DATABASE_URL value in the .env.local file. 

DATABASE_URL="mysql://dbuser:dbpassword@127.0.0.1:3306/dbname?serverVersion=8.0&charset=utf8mb4"

This should connect the application with a database. You can follow the same procedure if you want to use another database other than MySQL. 
Then I ran the following command and the MySQL connection worked and the database got created. 

$ php bin/console doctrine:database:create

Note: If the above step throws and error you can manually create the database. 

Creating Entity and Migrations

In Symfony we use the Doctrine library. This provides useful components to work with the database. To use ORM in the code to work with the database we need to install the following package. 

$ composer require symfony/orm-pack


Next I created migrations for the database. This will be done inside the src/Entity folder. This Entity folder gets created when we install the Doctrine package. 

The next step is to create an entity. At first glance it looks like the model in Laravel. 

$ php bin/console make:entity product

This creates two files. One inside the src/Entity directory and the other inside src/Repository directory. The above command also guides us to add columns to the table interactively. 

Once the entity and repository is created. I created the migration with the following command. 

$ php bin/console make:migration

This creates a new migration in the migrations folder. This migration files looks similar to the migrations file in Laravel. So if you are familiar with Laravel you will clearly understand the contents of the file inside the migration folder. 

Once the migrations are ready we can run the migration to create the tables. 

$ php bin/console doctrine:migrations:migrate

Updating the table can be easily done by editing the entity file or by running the same command that we used while creating the migrations. 

Fixtures can be used to load data into the database for testing. They are like seeders in Laravel. I am skipping this topic as I am comfortable with directly adding testing data to the tables. 

 

Writing Code

As with all my projects I did not worry about the user interface at the beginning. I need to build a simple application that shortens my long URL and once clicked or entered in a browser redirects to my actual long url. 

Code to Redirect Short URL to Long URL

public function url_forward($code, ShortUrlsRepository $repository, CreateVisitorsLog $log_helper): Response
{
        $short_code = $code;

        $url_details = $repository->findOneBy(['short_code' => $short_code]);

        if ($url_details === null) {
            throw $this->createNotFoundException('Url not found');
        }
        else {
            //record visits
            //$log_helper->create_log();

            //redirect to long url
            $url = $url_details->getUrl();
            return new RedirectResponse($url);
        }
    }


The rest of the code can be found in the Github repository


Why Did I Build This? 

I run a bootstrapped company and most of the time it is not financially viable to pay for all kinds of applications. This does not mean we buy no third party software. We do purchase, but before purchasing I mentally analyse what is the ROI in the purchase. If the ROI does not make sense then I go ahead and build it myself or use an open source solution. 

 

 

 

 




Like what you are reading?

Discover more similar articles sent to your email

Subscribe to my newsletter