Introduction to Symfony [ the famous php framework ]



About 4 weeks ago, I faced a challenge that felt like a mountain: Learn Symfony framework and build a decent web site from scratch with multiple users, logins as well as a backend interface to manage it all. Of course the web site had to have a beautiful design that ensures the best user experience ever.

 
Even though I already knew PHP I felt a little bit confused. I looked into at a lot of tutorials online and worked hard to finish my web site.
Here are few tips I recommend following if you want to try to work with a PHP framework such as Symfony in order to build your website.
First of all, why use a framework ? A framework is made to facilitate software developments by allowing developers to devote their time to meeting software requirements rather than dealing with low-level details. It is basically made to reduce your overall development time.
Should I learn PHP in order to be able to work with Symfony ? No. There is no need of you to learn PHP and master it to be able to work with Symfony. Like I said previously, a framework provides you a higher level of abstraction, which will allow you to code without dealing with a lot of PHP. You will be dealing with tested code and be able to code basic tasks more easily. Most C# programmers learn .NET along with it, so there is nothing wrong with learning Symfony without an experience in PHP. 
In case you felt uncomfortable and wanted to learn PHP first, I recommend following this tutorial https://www.w3schools.com/php/default.asp .
Symfony can be a bit hard in the beginning but once you spend a week or two working on it you will end up learning and loving it. There are few main thing you need to know starting Symfony.
1) The architecture : 
Symfony uses MVC architecture. MVC stands for Model, View and Controller. MVC separates the application into three main components :
Model : which is the shape of the data logic. Model objects retrieve and store model state in a database
View : View is the user interface. It displays Model data to the user and enables them to edit it.
Controller : Controller handles the user request. The user interacts with the view which makes a URL request. This request is handled by the Controller in order to render the view and data requested as a response.

Now that you understand how MVC works, let's take a look at the architecture of a Symfony project.
app : Contains the general configuration of the Symfony application, including the connection to the database, the configuration of the security.. etc
bin : Contains the executable files of Symfony and doctrine.
src : Contains your future source code.
tests : Contains automatic tests.
var : Contains cache files.
vendor : Contains external libraries such as Twig, doctrine and swiftmailer.
web : Contains Javascript, CSS and image files you need to make your web site eye-catching.

Under the src will reside your code.Everything under the src directory is organized by BUNDLE. What is a Bundle ? A bundle is similar to a plugin in other software, but even better. A bundle is simply a structured set of files within a directory that implements a feature. Each directory contains everything related to that feature, including PHP files, templates.. etc
The AppBundle is a default bundle generated automatically after you create the project. You can created as many bundles as you want. I named mine MyBundle. Once you create your bundle you will find Controller directory where you can put you Controllers in, and Resources directory where you can find the routing files and views.
You will have to create your own Entity and Repository directories which will represent the Model part of the application.

2) The routing :
Beautiful URLs are an absolute must for any serious web application. Having flexibility is even more important. What if you need to change the URL of a page ? How many links should you need to hunt down and update to make the change? If you're using Symfony's router, the change is simple.

When the user asks for a URL, a frontal controller handles it and sends it to the Kernel of Symfony. The kernel then send the request to the routing, which chooses the right controller from your source code. The Controller finally does the work explained previously : get the data from the Repository and displays it int the Views (.html.twig files).
3) TWIG :
Twig is a modern template engine for PHP which is fast, secure and flexible. It gives you the ability to display your HTML content dynamically in a very easy way without having to deal with PHP code. Your twig files represent the View part of your project, and reside under the Resources/views of your bundle.
4) Object-relationnal mapping (ORM) :
It is a programming technique for converting data between incompatible type systems to create a "virtual object database" that can be used within the programming language. Doctrine 2 is an ORM for PHP 5.4+. It send your entities to a manager in order to save it in a database. Your entities will reside under the Entity directory, and the managers under Repository directory. This will be the Model part of your bundle. The managers will use DQL (Doctrine Query Language) to select or update your data. It is very similar to SQL.

Learning a new thing may not be as easy as you think in the beginning. it requires patience, passion, commitment and a lot of research.

If you want to learn more about Symfony, you can check this tutorial (click-here) or take a look at the official documentation (click-here) on their website, It is a very well documented framework. You can also check this course (click-here) presented by the own creator of the framework. If you want to know more about bundles, you will find a lot of them here (click-here).

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel