Up and Running with Symfony 3.1: Installation and Basic Components

by Shahroze Nawaz  June 17, 2016

In a previous article, I covered how to install Symfony 3.0.1 on Cloudways servers. Recently, Symfony 3.1 was released and effectively replaces all previous versions. This release packs exciting new features and massive improvements to existing features of the framework.

In this series on Symfony 3.1, I will cover important aspects of the framework including a simple to-do app in Symfony 3.1, FOSUserBundle, user authentication using Auth0, upgrading Symfony 2.x to Symfony 3.x, REST API in Symfony 3.1, adding reCAPTCHA to Symfony forms, asset management in Symfony 3.1 apps, management of app bundles, and finally, migration from cPanel to Cloudways managed servers.

installation symfony 3.1

Similarly we can install Symfony 3.1 with a little change in command. What you have to do is move to your directory where you want to install Symfony. You can follow the above tutorial step by step to avoid any errors. While typing command of Symfony installation just replace 3.0.1 with 3.1 or just copy the below command and paste it in your SSH terminal.

Now it will ask you to enter database information. You can find it in your Server’s Application Management tab.

Now we are all set to make an app with the most famous and addicting Symfony framework.

But before we start making an app, it’s necessary to understand the Framework architecture. So in this tutorial, only the basic concepts of Symfony are going to be covered.

The MVC Pattern

Like codeigniter, Laravel, Slim, Symfony is also a MVC framework. It comes with neat and more precise directory structure with 3.1 update. So what’s the basic concept of MVC? It consists of three levels

  1. MODELS: This is the logic or information of the project in which the application performs its task
  2. VIEW: View is the GUI of the application which always renders our model on webpages. Views are mainly used to interact with the users.
  3. CONTROLLER: Controllers talk with user and respond with the actions. After taking response, it will invoke changes as defined.

MVC Pattern

Actions and Controllers

Symfony uses the actions and controllers like other MVC frameworks. Controllers are usually classes defined in our code with concatenation of two words “our controller name” and suffix “Controller”. Let’s take a quick example of it.

So DefaultController is our controller in this code every controller always extends the global controller class. Actions are methods defined in our controller classes. Actions are always associated with the URL when we hit specific URL it invokes the associated action on view. In this example, we are just calling a home page template.

Routing

Routing in Symfony is pretty simple. It routes the request by matching its URL which is defined in controller class.  Symfony uses annotations to define routing. Annotation is a simple and understable way to write a method without writing it in flat PHP code. See an example of how can we define annotation for routing.

It always starts with /** and ends at */
Between these syntax we define our routing method. @Route() method defines the specific URL which will invoke the execution.”/” is a URL path mostly used to invoke the home page. The second argument is optional. We can use it to name our routes.

Templates

Templates are the visual files containing HTML code for visual representation according to user needs and requirements. Symfony provides the render() method to invoke templates on specific URL request. Consider the following code line from above example.

We just have to define path to our template which is stored in app/Resources/Views/default/ index.html.twig’.

We can also make new templates and store them in new folder but they must be present in Views folder. When you open index.html.twig template you can see the below HTML code

Confused what is “twig” in template name? Basically Symfony uses Twig templating engine designed to develop modern and classy PHP applications. PHP is lacking in modern features for user interfacing so now Twig provides many features like named escaping and named arguments for filters and actions you can learn more from its documentation.

Environment

Every framework now comes with configurable debugging environment. Symfony also has one which is loved by developers. After running Symfony app, at the bottom you can see a bar. This is a debug bar for Symfony apps.

Environment

But what you see is just a scratch on the surface. You can click on different options and see many configurable items, issues, problems, HTTP headers. Symfony has two environments.

Dev: Useful when app developing locally.
Prod: Useful when app runs in production.

Symfony Localhost

What’s New in Symphony 3.1

Symfony introduces new and exciting features in its 3.1 release. Below are some features you can explore further from the official website here

  1.      LDAP
  2.      YAML
  3.      Process
  4.      Serializer
  5.      Web Profiler
  6.      Console
  7.      HTTPKernell
  8.      Form
  9.      Security

LDAP was introduced first in Symfony 3.0, but now it’s not an internal concept anymore. YAML also improves with many major changes like flags for parse behavior, support for parsing the binary tab, options to dump objects as maps. Input streams are added for processes, added a normalizer that supports JSON serialize objects. Web profiler is improved with more configuration items. Console is allowed to register command directly.

Final Thoughts

We have seen how flexible and easy Symfony is. We have covered the basic core concepts of Symfony in this tutorial. That wasn’t so hard, was it? It’s just the beginning! We have a lot more to discover in it. Also, we saw a code example for controller and actions. In the next installment of this series, I will demonstrate the capabilities of Symfony 3.1 with a simple to-do app.

If you have any query about this blog or Symfony, do let me know through the comment section.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...