Gaining momentum with Symfony2

By Eugenia Pérez | 9 August, 2015

I cannot yet reveal too much about the 3 technical books that I’m going to publish, but they will be pretty soon available to the public. So I’ve decided to write another tech book with practical concerns and real-world apps, based on the Symfony Framework. Hoping it’ll be an ideal complement for vocational education and training sector teachers to give server-side software development lessons based on PHP and also for those who want to achieve the SensioLabs Symfony Certification.

Since there are 16 topics in this certification test, I’m trying to structure a reference book guide close to that kind of structure, in order to facilitate a proper tracking and a a successful preparation (with determination…) Topics are basically these ones: https://sensiolabs.com/en/symfony/certification.html

Some of the features that have caught my attention after deepening, elaborating and enriching the contents are:
- A cool feature that Twig provides is what it’s called filters. What this means is that Twig provides a mechanism to transform the content which is going to be displayed just before it happens. This is something that some other frameworks also include like for example AngularJs, while the latter does it client-side. There are some built-in filters which you can use just out the box, such as upper, lower, etc.

{% extends 'base.html.twig' %}

{% block title %}Days of the week!{% endblock %}
{% block body %}
    <p>The days of the week are:</p>
    <ol>
        {# Prints out the list of days #}
        {% for day in daysOfWeek %}
            <li>{{day | upper}}</li>
         {% endfor %}
    </ol>
{% endblock %}
{% block footer %}
    {{parent()}}
    <span><a href="http://eugeniaperez.es">EugeniaPerez.es</a>
{% endblock %}

- As all we know web apps are accesed using a wide range of devices. Mobile devices are widely used by users to consume contents. This is why it’s essential to provide a convenient way to browse our sites. With Symfony and its routing module we can easily determine what kind of device is behind an HTTP request. Of course this can be done inspecting the User Agent HTTP header. In that way we can search for keywords such as Android, IOS and so on to identify mobile devices.

/**

* Returns the product details for mobile devices.

* @Route("/products/{id}", requirements={ "id" : "\d+" },

* condition="request.headers.get('User-Agent') matches '/Android/i'")

*/

public function mobileShowAction($id) {

     return new Response('<h1>Mobile version site</h1>Product #' . $id .'requested');

}

/**

* Returns the product details.

* @Route("/products/{id}", requirements={ "id" : "\d+" })

*/

//@Route("/products/{id}", requirements={ "id" : "1|2" })

public function showAction($id) {

     return new Response('Product #' . $id . ' requested');

}

With the condition presented above we are matching all the incoming devices that contain Android in their User Agent. Then we can have another action without the condition to serve contents to the rest of the clients.

facebooktwittergoogle_plusredditpinterestlinkedinmailby feather