How to create a custom Twig function in Symfony 2

Hi all,

Did you ever wonder of creating your own custom twig function. For instance, assume that you are displaying set of url’s in your view (twig) file. Suddenly you want all or some of your url’s to be opened in a new tab once clicked. We all know this can easily be done by having the target attribute in anchor tag such as <a href = “link” target=”_blank”>Text</a>. This seems to be pretty straight forward. But if you want to add or remove this target attribute one day in all of your urls, it will be nightmare to change all down the line. Instead, if you have used a custom twig function to add this property to the link and use that function instead of html anchor tag in the twig file, things will be much easier when it comes to changing things later.

Let’s see how we do this,

  • To achieve what we wanted, we want to create a new class (which extends Twig_Extension). Create a new PHP class and save it in a your project. Define the class name extending ‘\Twig_Extension’. (Make sure you add the namespace)
  •  In the class file, you need to declare two functions. One is to register the custom function and one is to return the name of the class as a string. The first function you need to add is ‘getFilters()’ and register your custom function there.

public function getFilters()

{
return array(
new \Twig_SimpleFilter(‘custom_website’, array($this, ‘customWebsiteFilter’)),
);
}

Continue reading

Share

SQL into DQL, examples | Symfony 2 | Doctrine 2

Hi everyone,

Today I am going to talk about some examples on converting SQL queries into DQL . Doctrine query language (DQL) is the query language used by Doctrine ORM. Doctrine ORM 2.x is shipped with Symfony 2.x by default. Let’s take some examples and discuss. For the following examples we will be creating an instance of Doctrine query builder to structure DQL’s. 

Creating an instance of doctrine query builder

$em = $this->getDoctrine()->getManager();

$result = $em->createQueryBuilder();

SQL

SELECT * FROM contact

DQL

$dql = $result->select(‘c’)
->from(‘ACMETestBundle:Contact’, ‘c’)
->getQuery();

Continue reading

Share

Extending Symfony 2 Web Application Framework Book Review

Hi all,

Recently I found a book which is related to Symfony 2. The book is called Extending Symfony 2 Web Application Framework by Sebastien Armand. So I made a brief review about it. Find my review below.

I would say, Extending Symfony 2 Web Application Framework by Sebastien Armand is one of the few knowledge sources out there in the market which seriously discuss Symfony 2 in many directions. The book is organised in valuable six chapters and guides you through a real time social website (meetups) example. These chapters contains many deep discussions regarding Symfony 2 services, listeners, dependency injection, commands, templating engine including Twig, database layer of Doctrine including creating a your own data type and so much more. Also this book covers a handy discussion regarding ensuring the security and increasing the performance while maintaining a good solid code. As an overall, this book is ideal for someone who is already familiar with Symfony 2 framework, but loves to enhance and polish their thorough understanding about Symfony 2, tailor / structurize pre build applications using much effective approaches and developing easily shareable bundles.

Where you can buy the book :- http://www.packtpub.com/extending-symfony-2-web-application-framework/book

Cheers!

Share

Create your first route | Symfony 2 | Beginners guide

Hey Symfony 2 lovers,

Let’s have a good understanding about routings in Symfony 2. Routing is the navigational paths (directives) in your application. For instance if you want to show all your products in your shopping cart application, which was developed by Symfony 2, routing could be either ‘your_cart/web/app_dev.php/show_all_items’ or ‘your_cart/web/app.php/show_all_items’. In here ‘your_cart‘ is the name of your application, ‘web‘ is the default path in Symfony 2 (you can hide this later), ‘app_dev.php‘ and ‘app.php‘ are two environments in the same application, such as development and production environment. These environments are there to distinguish your application in different purposes which I am not going to discuss any further. Our main concern in here is the ‘show_all_items‘ bit. This is a routing which fires a function in a controller. If you are new to a frameworks this concept might be little surprising to you. This is how the major frameworks work. When you hit a path, such as ‘show_users‘ will call a predefined function in a predefined controller. If you see something like this ‘show_users/10‘, probably that is to show how the user which has the id of 10, something like that. This 10, will be passed to a method as a parameter, such as ‘public function showUser($id)’.
Continue reading

Share

Injecting Services in Symfony 2

Hi all,

Let’s have a quick but precise look at injecting various services in Symfony 2 today. Before going any deep, what does it mean by injecting services ? Well, if I am not so wrong, not many PHP frameworks do favour injecting services as Symfony 2 does. Injecting various services into the application is a really beautiful, cool thing, I would say. If you manage to inject services properly, you really don’t want to take care of object creations. Well, what’s injecting service actually is ? Injecting a service in Symfony 2 is, you define, a service or set of services (in PHP, classes) at the application load time, then you reuse those services as much as you want, any time during the execution, without redeclaring or creating objects of those services (classes). In other words it will help you to “standardize and centralize the way objects are constructed in your application” . There are 3 types of injections in Symfony 2, which are,

  1. Constructor Injection
  2. Setter Injection
  3. Property Injection

In this post, I will be discussing about the ‘Constructor Injection’, because I thought it might be useful to know how this type of injection works before knowing the remaining two.  Continue reading

Share