Five months with Symfony 2

Hi folks,

Well, today I am going to briefly discuss on couple of things which I have learned throughout my journey in Symfony 2.  Well the journey is still ongoing and this brief discussion is about first couple of months, in exact terms first five months.

At very beginning, as an experienced CodeIgnitor and CakePHP developer, I found that Symfony 2 is quite confusing for me. The bundle concept started to bring some nightmares on me. The ‘bundle’ concept is a whole new experience for a developer who is sound with PHP frameworks such as CodeIgnitor and CakePHP. But today I found that, I am so lucky to get my self around with ‘bundles’, which makes developers life very straightforward and easy. I honestly believe Symfony 2, SensioLabs, Fabien did the correct thing by introducing ‘bundles’ for Symfony 2 which was not there for Symfony 1.x versions. Bundles let you organise your features, functions within your project in most efficient manner. The knowledge of arranging your projects according to functions, features within bundles will come to you once you spend sometime playing with Symfony 2. Then you come to a point, you get a feeling that how the things should get arranged in terms of Symfony 2, bundles point of view. So try your self by playing with Symfony 2, creating bundles under various vendors, etc, try your self how the bundle is created.

Continue reading

Share

Running console commands using Process component in Symfony 2

Hi All,

Today I am going to talk about process component in Symfony 2. Process component is a useful library which can be used to run console commands as it is. The main driving factor to discuss this topic is, recently I had an issue in running multiple console commands in Symfony 2. I tried using doRun method in Application. It worked for commands like doctrine:mapping:import and doctrine:generate:entities but not for doctrine:generate:form.  Even it worked for doctrine:generate:entities, I didn’t managed to generate a single entity in a bundle. I had to generate all entities for the whole bundle. In other words “php app/console doctrine:generate:entities ACMETestBundle:Person –no-backup” did not worked. So I had a really difficult time with this until someone suggested me about the process component when I posted this question in StackOverflow.. To be honest, I didn’t had the opportunity to explore process component thoroughly before that. But I found that process component is a really cool library which we can use to run console commands in Symfony 2. I thought it might be useful for someone who is trying to run console command using the application. If you have struggled running console commands using Application’s, run or doRun methods, this post is especially for you.

Continue reading

Share

How to create a custom Twig filter 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

How to install ApiGen in NetBeans 8.0 using PEAR

Hi All,

Today I am going to show you how to correctly install ApiGen in NetBeans 8 IDE using PEAR. Well, ApiGen is a handy, powerful and customizable code documenting tool which will help you to generate code documentation for your source code. So let’s begin installing ApiGen in to NetBeans.

  • First, you need have PHP installed in your machine and environmental variables set for the installed PHP. (If you need any help in installing PHP or setting environmental variable, just drop me a comment below, I will help 🙂 )
  • Open up your command prompt in Administration mode (right click and run as administrator)  and type this command to enable PEAR auto discovery mode. Auto discovery mode will help you to automate dependency installation. Because ApiGen requires tools such as Texy, Nette and so on. The good thing is when the auto discovery mode is on, it will install all these dependent tools automatically for you.
    pear config-set auto_discover 1

    apigen Continue reading

Share