Website Design

Build a Chat Application With Silex Using PHP

Rushik Shah User Icon By: Rushik Shah

This article is all about building a chat application using Silex; though it is more than just building a chat application. The aim of the article is to familiarize you with Silex. Thus in case you have not come across Silex prior to this, Silex is a PHP micro-framework which has been built for PHP5.3. It has been based on Symfony2 and Pimple and is also inspired by Sintara. It is intuitive and fun to use and has an extension system that is based around the Pimple micro-service container making it easier to tie in third party libraries.

Silex uses the Symfony2’s Http Kernel which abstracts request and response. This feature of Silex makes it easier to test apps and the framework itself. Thus while many PHP developers in India are still only getting to know such technology we at Alakmalak are aware of such technology and know how to utilize it for the best of our clients. Alakmalak is a company where web design and web development are taken seriously and done methodically. We are aware about the need to separate the design from the functionality and strive to achieve that by implementing good and proven frameworks where possible.

Make Chat Application - Silex Using PHPMake Chat Application – Silex Using PHP


A micro-framework is like a regular PHP framework stripped to the bare bones. Many of the frameworks are massive and so large that they are hard to handle. That may be overlooked if your project is a large one and uses many different features that would be usually found in such a big framework. On the other hand if you really only need to build a small applications in PHP and want to use a framework it is kind of unnecessary to carry excess baggage so to speak.

Hence micro-framework is the striped down version of a regular framework that can get all the things done and in a nice manner without the extra features that you generally don’t require in a small application. Micro-frameworks encourage swift development and can be incredibly useful when a quick site is needed. There are several popular micro-frameworks around. Silex is one such micro-framework that is strongly built upon the powerful Symphony components.

Template Engine:

Implementing a template goes a long way in helping separate our concerns, so that the designers and the developers can work with the dynamic web pages in a more maintainable way. For those who have considerable experience either a designer or a developer they must have come across web pages during their career that mixes both the HTML code meant for the layout of the site with PHP code that is meant for the functionality of the website.

This is perfectly fine if it is a very small application or piece of code. But the second we try to build a decent application where slightly complicated procedures are involved along with complicated designs this kind of practice can make the code quite messy and hard to maintain, if not impossible. Thus comes the template engine to the rescue.

The template engine is a piece of software that takes the template files and renders them as HTML. A template file is similar to HTML and PHP files except that the PHP syntax has been replaced with a template language syntax. The advantage being that since it is set use along with HTML it looks much cleaner and can even be helpful in rendering dates. The template language that will be used along with Silex is Twig due to its easy integration with Silex.

Twig is a modern template engine for PHP which compiles templates down to plain optimized PHP code. It is highly secure and gives the users the ability to create their own custom tags and filters.

Silex Project Structure:

The directory structure needs to be set up to start with. The /src/ directory will contain the code. We have three directories within the /src/ directory and they are /public/, /private/, and /server/. All our backend PHP code will be placed in the /private/ directory, while the JavaScript server is going to be placed in the /server/ directory.

The /public/ directory will contain the publically accessible elements of our project. All the imported code will be placed separately from our own code. The /vendor/ directory will store all our dependencies.

Directory Structure Of Our Project - Silex Using PHPDirectory Structure Of Our Project – Silex Using PHP

We shall now be using a component dependency tool. The composer website provides instruction on how to quickly install it via the command line. It is possible to download composer.phar from Packagist ( You can then follow the instructions and install it.

Alternatively you can also download Composer-Setup.exe for windows from and simply follow the installation instructions. Composer is a dependency manager. Composer is a smart tool that seeks the dependencies in your project and then downloads them into your project.

For example you have a project that depends on a number of libraries and that in turn depends on many other libraries. You can go ahead and declare the things you depend on and Composer will find which versions of which packages need to be installed and then download them into your project.

We first need to create a file called composer.json in the root of the project that tells the Composer which dependencies we want to install. Then open the command line at the root of your project and run composer file by typing a command line. Running this command with the suitable options will download your dependencies and place them into a /vendor/ directory.

Building the index - Silex Using PHPBuilding the index – Silex Using PHP

Building The Index:

We now need to make an index.php file in the /src/public/ directory. This will be the file we point our browsers at. It simply requires another index.php file from the /src/private/ directory. This is simply to a security precaution so that the internal sensitive data is remains hidden.

Code for the index.php file (/src/public/):

OVER 600 Small, Medium and Large Enterprise business clients have chosen us from 35 COUNTRIES

  • Federico Bilches

    I want to thank all Alakmalak team for take my project and help me to improve my website! Thank you for your responsibility and seriousness at the time to do corrections and improvements on my website and for always be in touch with me. Highly recommended! Great team! Great company! I'm very thankful Alakmalak!

  • Frank M. Cali
    Frank M. Cali.USA

    A few kinds words to say about Alakmalak programming and design. Working with these guys is always such a great relief. You know you are getting your stuff done right and usually always in a timely fashion. I’m a middle man for the most part And my clients are always happy with the end result.