Re-frame your work or just Compose it – PHP Frameworks

In this post I will talk about the trending PHP frameworks. Over the past 6 months I have been hearing a lot about the Laravel Framework. Being a more database focused developer I have tended to use Yii. After exploring Symfony 3 and the doctrine component I started to like the way it was put together; lets start with the new version of Yii version 2.

Why are frameworks so important? This question has many factors and one has been the slow change from bespoke projects or systems to communally supported projects or systems. With more and more support for a framework, the better the stability of the end project or system. With more problems being solved the greater need for a robust and efficient updater or upgrader is required. With the new version of the frameworks now supporting composer, a PHP package manager the simpler it has become to update the components within.

Starting with a new project it is always best to install your packages before you set off into the land of programming. So using composer I installed Yii 2.0

composer create-project –prefer-dist yiisoft/yii2-app-basic basic

From there I did a little experimenting, composer is a PHP package manager so therefore when installing packages the manager should work out the package dependancies. On a current project at Linear Blue I have been working with the eBay PHP SDK which is installed using composer, along with this I also installed Codeception for Acceptance testing. Installing these two packages I ran into a road block. Installing Codeception then the eBay SDK resulted in a dependency clash. How does one resolve this. The report suggested removing the dependant package and installing again however this didn’t solve the problem. The solution was to remove the Codeception package install eBay and then reinstall Codeception.

So composer doesn’t come with out any quirks. One which baffles many I think is the path redirection, for instance the eBay path to the files would be vendor/dts/ebay-sdk-php/src/{files and folders of the SDK} but with in the main vendor folder a file called autoload.php resides. Including this file in your project at it’s immediate start point will allow the developer to quickly include the files needed. How does this work! Each package has a file composer.json, in this file there is a declaration autoload. Within this declaration the PSR standard is also declared, the current standard is PSR-4 which outlines the loading specification. In most cases this can look like the directory listings are being rewritten when really they are not. Following the structure to which the resulting class adheres to in its namespace the eBay example for instance the namespace structure starts DTS\eBaySDK as per the declaration from the composer.json file. A class file will add the namespace DTS\eBaySDK\Trading\Types at the top of the file followed by the class definition with the keyword “use”. When a developer wants to use this class or any classes under the namespace umbrella they simply put at the top of there file “use DTS\eBaySDK\Trading\Types” and call the class as follows $var = new Types\AbstractRequestType();

From Yii’s point of view from the very start point the autoloader is loaded and therefore you are ready to load any package you have installed.

The initial setup of Yii I did twice due to running into the dependency issue more investigation is required to safely remove packages, upgrade or reinstall them. So far as per the previous version 1.x it is quick and relatively simple to get up and running. One of the biggest changes I found was the components directory has now been dropped and composer has been adopted, however the advanced version is some what more complicated so will cover that another time.

Let’s look at Symfony 3; firstly has it’s own installer so we need to download that and set it up the recommended configuration for a Linux or Mac installation as follows:

$ sudo curl -LsS -o /usr/local/bin/symfony
$ sudo chmod a+x /usr/local/bin/symfony

With the installer downloaded and installed we can add a new project.

$ symfony new my_project

This will add a clean project.To try things out run…

$ symfony new demo

Which will add demo content, but as we are going to start by looking at what we have I will keep it clean.

There are many aspects to Symfony; I will concentrate on just getting things up and running. With the installer running the command line will be displaying information as it installs. Once it has finished it will display a method of running the application. Symfony comes with the ability to run the application without the need of a web server. Within the project directory there is a bin folder which contains an executable console. by typing php bin/console server:run providing you have php installed will run the application. For my system I am running XAMPP so I will skip this.

Looking at the directory structure it is clear that Symfony is tightly integrated with composer. To get started the main AppBundle directory src/AppBundle holds the main project files. Bundles are beyond this blog but Symfony has the ability to create bundles of code/functionality which done correctly can be portably between projects.

The project starts off in the web directory with one of two files app.php or app_dev.php for production app.php would be used. This file then sets up the autoloaders this is for both Symfony’s bundles and composer. Because a lot of the components are now situated in composer this has to be included first the include is a little hard to follow at first. Knowing that the vendor directory has an autoloader class it would be assumed that this is included in the app files but what happens in the file is included from the folder app and autoload.php this then shows that composer is tightly integrated right within the Symfony structure and one can not work with out the other.

Once you understand the structure it becomes clear and simple as to where packages/components are located, making deployment much quicker and simpler as the main composer.json file holds all references to any components used within the project, much in the same way that Yii is done but the difference is Symfony is forcing the developer do use composer or the project will not work. One package that is required if connecting to a database is Doctrine.

One of the main features I like with Yii is the CRUD generator with Symfony it is more command line based the excitable console in the bin directory holds a lot of power from creating new bundles to creating the CRUD classes the Doctrine setup is beyond this blog but to get a feel of the power have a look at the following URL

So far comparing the two installations it is clear that Symfony holds a lot of power, freedom and flexibility but with all that if a developer does not follow the structure or the rules it is possible to get lost in the complexity. At this point between Yii and Symfony it is clear that Yii is in a transition period it has some good features to help a developer get up and running quickly but it seems as if adding composer has been a last ditch effort to solve a problem of keeping components/packages updated from an application migrating from 1.x to 2 there would be a lot of effort involved and possibly some rewriting. In my mind Symfony is much more robust and future prof and seems to have solved the problem of portability and package updating. There are many other features that could be talked about but from a starting point Symfony is a great framework to get any project off the ground.

Having used the two frameworks previously mentioned I now come to Laravel this is a framework which as of yet I have had no experience with so lets start by setting up a project. To start with you can install the installer much like Symfony

composer global require “laravel/installer”

From there you can create a project

laravel new my_project

I have chosen to use the composer route as at this point don’t want to install the laravel installer.

composer create-project –prefer-dist laravel/laravel my_laravel_project

Interestingly the first thing to note during the installation is that composer is installing most of the Symfony framework. At the end of the installation you are presented with a long list of suggestions regarding installing further packages and composer or the Laravel command will automatically create an application key which is used to secure sessions and encrypted data.

After the installation process is complete make sure that the storage folder and sub levels are writeable and Laravel is ready serve.

Much like Symfony there is a console application called artisan. At the command prompt in the project directory type

php artisan list

This will list the available commands, two commands which are the most important are the up an down commands. Putting laraval in down mode puts the framework in maintenance mode and running the up command will turn off maintenance mode. In an age of constance up time there at times needs to be some down time but that doesn’t mean with Laravel that the website needs to be fully down, presence is always maintained.

Laravel is more or less Symfony so why the need for another Framework. The only answer I could come up with is that Laravel has been built with data driven heavy traffic oriented sites in mind with memcache and Redis drivers out of the box ready to use and has a built in migration system that is platform agnostic, all of this is not available to Symfony out of the box, but that said it is not really a deal breaker in respect to Symfony or Yii, both offer very powerful tools. The proof is when creating a new project and how quickly one can get up and running. So far all three have been quick to actually get started.

For an out of the box Framework Laravel does have a lot to offer but it won’t suite very project. The services it has to offer are quite extensive one which focuses on performance is the Elixir service which aims to combine your JavaScript and CSS files into one file respectively thus creating one call instead of calling lots of JS or CSS files.

Without getting too far under the hood of these Frameworks it is clear that all three have grown into accessible and reliable software applications that can really help in creating that next big thing. To continue this I will need to create a project with all three Frameworks to compare and contrast the use and development process. Composer has really changed the way PHP development projects are now produced, maintained and deployed something that was much needed.

Darren Kayes

Darren is Linear Blue's Chief Operations Officer, ensuring the smooth running of the company and making sure nothing gets in the way of our developers creating top-notch web and database solutions for our clients.

More Posts - Website

Leave a Reply