Worklog Part 2: Creating navigation with bootstrap

Creating navigation with bootstrap

We were start from already prepared bootstrap theme sample and we will try to take as much as possible out of it.


Navigation bar will be at the top of each application page and should be always visible. All shared elements which are on all pages,  are going to the base layout, this is base.html.twig in our application.

If you need some specific information about bootstrap you can probably found it on this site. site is one of my primary sources for web and SQL related questions.

After some experimentation I create something like that:

2015-12-06 12_18_10-Dashboard - Bisaga Worklog

Main navigation bar at the top is always visible even when we scroll down beyond visible space (navbar-fixed-top). This require little additional CSS settings that our pages on the top has some default margin.


In our worklog.css we define default top margin as:

Because we define “body” element as selector, this will be valid for every web page in our web site.

Navigation tag with bootstrap  settings

One part of template looks like:

We create navigation bar with brand icon on the left and some icons with two direct links (calendar, alerts) and two drop-down menus for some additional actions.

You can download the whole file from here.


Default page on the web site will be used as dashboard, so we create dashboard.html.twig template file and controller class in  DashboardController.php . We also connect default ‘/’ route to this controller (Application.php).


For links we use direct path navigation with little help of Silex Application constants, so links are assembled like this:

In some point I will probably study assetic component from Symfony but for now I will stick to this simplest semi-direct references.


Icons are actually characters from the font Font-Awesome. Images on the navigation bar are slightly larger (fa-lg) then those in drop-down menus.

You can learn more about font awesome usage here.

This sample application is published on github.

Worklog Part 1: Building web application with Bootstrap

Integrating Start Bootstrap template

We will implement bootstrap based web application theme in Silex application. The best way to start out is with already available theme.

To install it into our application just execute bower install command in application web folder:

After installation of all required components (including jquery, bootstrap  and a few others) we will need some help how to use them. You can download sample implementation and start learn directly from included sample application.

After unzipping sample into local www folder we can navigate to it and start inspecting how it is constructed.

2015-11-26 19_08_40-SB Admin 2 - Bootstrap Admin Theme

This beautiful sample contain almost everything what is needed to build very powerful web application !

Next step is converting part of this sample code into our application.

Integrating template in silex application

We need to analyze index.html file from the sample and transfer definitions into our base.html.twig file.  As you can see in the content of twig file, we only copy “link” and “script” nodes to our file and adjust locations of used resources.

After you have everything in place the response after loading index.php should be something like this (Firefox/Developer tools/Network screen):

2015-11-26 19_46_09-Network - http___localhost_worklog_web_index.php

All resources should be loaded successfully .

The content of twig base file:

Worklog application folder now grow little larger because of new front-end components in bower_components folder.

2015-11-26 20_03_12-Programmer's Notepad - [index.html]

At this point our web application is almost empty, but we have all  necessary components ready to use.

This example application is available on the Github.

How to add data migration support to Silex

Add Doctrine data migrations to Silex application

There are quite a few blog articles about using doctrine migrations in different project types and this article was closest to what we need in Silex project.

Install doctrine migration module

We add a few required components to composer.json and execute update command.

We need to have symfony/console module already installed and prepared for extending with new commands, as I wrote in this article.

Add migration commands to console.php program.  You can find them in vendor folder under:  “/lib/Doctrine/DBAL/Migrations/Tools/Console/Command” path.

Add migrations configuration files:

Migrations configuration define base settings for migration generation and execution environment.

Database configuration is needed that migrations can really do the job.

Default table options

How to set default create options for tables and columns such as default collation sequence and character set ?

‘defaultTableOptions’ => [
‘charset’=> ‘utf8mb4’,
‘collate’ => ‘utf8mb4_slovenian_ci’

To set custom character set and collation just add “defaultTableOptions” array with your specific  default settings in connection configuration array (file: migrations-db.php).

Console command interface

You can test it immediately with console command:

>php console.php list migration

2015-11-18 21_47_07-Command Prompt

With data migrations you can easily update your project database with a programmatic interface with version support.  Your database schema can grow without risk of loosing control over it.

Sample script

Empty class is generated with “migrations:generate” command. After initial generation you code migration changes manually.

After generating first migration file the file is saved in configured migrations folder (./app/Migrations/).

2015-11-19 00_53_25-silex03 - NetBeans IDE 8.0.2

Run migrations

Your migration scripts will not running automatically, you need to do it by yourself.  To execute open migrations you need to run command:  “migrations:migrate” .


Adding symfony console support to silex application

Adding console support to Silex application

Silex is a micro-framework and as such doesn’t force you to use so many components included in full stack symfony framework. But, if we want to use some of them we need to do some extra work.

Install console component

Installing with composer is simple, just add additional requirements for “symfony/console”  to composer.json file. The full content of my composer.json file as currently is :

After changing composer.json file just don’t forget to execute update command.

This way all required components for console support are installed. We can always check “vendor” folder after update, there will be everything required by composer definitions.

Creating main console program

To be able to use “console” command, we need console.php program first. We have very easy job here, because everything is already written, we just need to get it from symfony site .

We create console.php file with following content:

As you can see, we already initialized one sample command here, “GreetCommand”, we need to create the command too.

Creating sample command in php

My silex application is as simple as it can be. The best in using micro-framework is that there is no forced structure from framework. We can create our own folder structure, so my is something like that:

2015-11-15 20_22_50-Programmer's Notepad - [console]

My  folder structure is not yet complete, it will evolve as application grows.

We place GreetCommand.php file from symfony web guide to our Infrastructure \ Console folder and change namespace to reflect our folder structure.

Now we can test new command line program simply by calling console.php program with demo:greet parameter:

We put console.php file directly to the root folder of our application, so we call console command from that folder.

If you wish to simplify call to the console.php program, you can write simple  batch program and put it somewhere in the system path:

Now, you can call any console command as :

For checking which commands are already initialized in the application, we execute “list” command:

2015-11-15 21_00_19-Command Prompt

That’s it !

We are ready to add any commands to our silex application !




Cron job – running PHP in the background

Background jobs

If you wish to write responsive web applications, you will need to  push some operations in the background. That way you can just register request for some long running task and immediately return to the client.

If you search the web, there are many ways how to achieve this, but not so many implementation are ready to do it in constraint environment of simple web hosting.

My web page is hosted by GoDaddy with so called “Linux hosting with cPanel”. I have PHP and MySql, but not much beside this. GoDaddy luckily allows cron jobs.  We simply register some command as “cron job” to run unattended at specified frequency.

For proof of concept I will need simple php program and run it as cron job.  At each cron job iteration we will insert one record into database table. We just want to proof that php program can run in the background as  cron job.

Create some database and add “tasklog” table.

Our simple PHP program is :

To test it, just put the “taskrun.php” in your “public_html” folder and navigate to it. If something will go wrong in the program, the settings for exceptions are set to report it to the client. Please test the program until everything not running smoothly.

Register cron job

You can put program file to any folder. If folder is not under “public_html” folder, it will be inaccessible from the public web and that way will be much more secure. We create new “jobs” folder under our root home folder and move “taskrun.php” program there.

In the “cPanel” locate “Advanced” section and select “Cron Jobs”:

2015-10-22 22_42_41-cPanel - MainCreate new job with a one minute frequency as:

To prevent email to be sent for each iteration, we put redirection into the command ( > /dev/null 2>&1 ).

2015-10-22 22_45_26-cPanel - Cron JobsWait a minute and check if there are some records in the “tasklog” table. You will see something like this:

2015-10-23 _ localhost _ bisagasamples _ tasklog _

Success !

Of course, this is only proof of concept for running something unattended in the background. But I think there are already some open source job-task runner out there.