To start some kind of Symfony 2 series articles, I chose this as the first theme for articles about that great framework.
What is Symfony anyway? As official Symfony 2 page says:
Symfony is a PHP Web Development Framework…
Is this enough for you to get started with development with that?I prefer to say that it is the MVC based CMS framework because of structure based on, and rules you have to follow when developing, so code is cleaner and all is on right place. Also there are strict rules for best practice programming that is easier to follow when developer is some kind ‘forced to do things right’, and that is the case in Symfony 2. But, when you once get to know better that logic, you will love that!
I would like to write in short some common features and ways of using it and I am sure that most of you after that will decide at least to try.
Installation is very simple to do. Just download latest version from their official page or git repository (you have versions for download with or without vendors, but for beginners I suggest to download with vendors for start). After downloading put the source in web root on localhost and ready to go.
Of course, there some prerequisites that is necessary to use it: Symfony 2 is working on minimum version php 5.3.x. So if you don’t have php 5.3.x installed, don’t bother with installation of Symfony…
If you enter: ‘www.yoursite/yoursymfonyinstallation/web/config.php’ then you have option to set up your database configuration through user interface without writing any additional line of code.
There are three basic possibilities for basic configuration:
– yaml files
– xml files
– plain php files
so, it’s on developer to choose method for configuring application. I am talking about that because lot’s of things in basic setup can be done through configuration files without writing much php code to get something work.
So in configuration files there are options to configure database connection, CSRF protection for forms, validation, templating, translations, sessions, mail sending, etc, etc. For routing there are separate config file and also for security settings so it’s very easy to config some basic security for out web application with that.
But there are one more thing that is interesting: Symfony 2 also make good sense of using so called “Annotations” in form of php comments to configure something so that give us a possibility to fine tune our web application both in config files and our php code. It’s subjective choice to make what type of configuration we will use in our project.
In standard package downloaded there is a Acme Demo Bundle where we can see some basic code of simple application that can help us to start writing code and understand better system of code organization in project.
Source code is intended to be in ‘src’ folder organized in sub folders – name-spaces: NameOfYourCompany/SomenameBundle/ …
In Symfony 2 everything we make have to be organized in so called ‘Bundles’. Symfony also uses php5.3 namespacing so don’t forget to include namespacing conventions in your code.
In your bundle folder there are sub-folders ‘Controller’, ‘Resources’, ‘Entity’ and so on in which we should place our classes depends of class type and function.
Also there is one more great feature called ‘twig’ engine so when we make our templates of UI, we can decide if we want to implement it in ‘twig’ format or in plain php. By default Symfony 2 offers us to use power of ‘twig’ php templating engine.
Until now I didn’t mention database features. By default, Symfony2 uses Doctrine 2 for database interaction and all database logic is that way separated from businesses logic which make it great to use.
Doctrine 2 supports many remote database providers and also works great with MongoDb.
To be honest, I am working with MySQL database and I am completely satisfied with that.
Also in Symfony 2 is implemented a console tool for managing different aspects of application including support for Doctrine 2 CLI and can be used without much effort for configuration.
Unit testing with phpUnit is relatively simple task to implement. All you have to install some PEAR packages for phpUnit and add some global variables in your php ini and ready to go.
There are so many features to write about and all of them will not have place in this first article, but I will write about these things later in future articles.
If you are not satisfied with this article, and not yet decided to start with Symfony 2, please wait for more and then decide…
On the end of article, I just have to say: “Thanks very much!” to my colleague Branko Ajzele for helping me enter into the Symfony 2 world and helping me to overcome all obstacles in my way to use Symfony 2 for my current project.