Installing Symfony doesn’t take long, but depending on your Composer capabilities on your web server, you may run into issues if you don’t install LESS and Node properly. Find the Node modules path by typing in the Linux command line:

find / -name node_modules -type d

Then set to the node_paths option:

assetic:
  filters:
    less:
      node_paths: /usr/lib/node_modules
      compress:   true
      apply_to:   "\.less$"

To test Assetic and Less together, in command line, run this to see if there are any debug errors:

app/console assetic:dump --env=dev

Cycle through an array of values for current year and 5 years into the future:

{% set start_yr = date() | date('Y') %}
{% set end_yr = start_yr + 5 %}
 
{% for yr in start_yr..end_yr %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}

The array “colors” would probably be better off defined in the controller then passed into the view here, but here’s an example of how to set and initialize a new array in Twig. This can be handy if you are setting certain display options in the view that’s not tied to any data or logic that would be used anywhere but inside your views.

{% set colors= ['red', 'orange', 'blue'] %}
 
{% for i in 0..10 %}
    {{ cycle(colors, i) }}
{% endfor %}

Here is an example of a simple DQL query in a custom repository method meant to return a single array result. The following will return the latest result in case there are multiple contact records that match the “customerId” field, which is actually “customer_id” in the database, but remember that camelCase convention applies to Doctrine mappings of database fields.

// inside entity repository is the getMostRecentContact() method which is called on from any controller that has initialized this repository

function getMostRecentContact($customerId){

   $fields='c.firstName,c.lastName,c.email';

   $dql="SELECT $fields FROM \Main\DefaultBundle\Entity\Contact c 
         WHERE c.customerId=$customerId AND c.status='Active' ORDER by c.createdAt DESC";

   $contactArr=$this->_em->createQuery($dql)->setMaxResults(1) ->getArrayResult();

   return $contactArr;
}
Note: Using Symfony's built-in repository method findBy() is possible here, but with multiple "WHERE" conditions, this approach is usually more flexible.

In your controller, you can access the special global “_locale” variable from the Request object:

$request = $this->get('request');

$locale= $request->getLocale();

 


In Symfony, cookies are stored in the app.request object. Here’s how you access them in Twig:

{% app.request.cookies %}


To see if a cookie is set, use the if statement inside Twig:

{% if app.request.cookies.has('yourCookie') %}
  Cookie 'yourCookie' has been set.  {{ app.request.cookies.get('yourCookie') }} 
{% endif %}