Checklist before you upgrade your symfony2.0 to symfony2.1 version

Symfony upgraded version comes with lot of fixes issue. They have added lots of new features but i felt disappointed in terms of performance. The performance has been falls around 30 to 40% in symfony2.1 version.

The following things has been changed:-

1. Doctrine

The DoctrineBundle is moved from the Symfony repository to the Doctrine repository. Therefore you should change the namespace of this bundle in your AppKernel.php:

Before: new Symfony\Bundle\DoctrineBundle\DoctrineBundle()
After: new Doctrine\Bundle\DoctrineBundle\DoctrineBundle()

2. HttpFoundation

Locale management was moved from the Session class to the Request class.

Configuring the default locale

Before:

framework:
session:
default_locale: fr

After:

framework:
default_locale: fr

Continue reading

Top 5 Symfony2 Bundle

In a small time Symfony2 is on the top position among the huge list of php frameworks. Fabien has tried to make the framework fast,pure OOPS oriented and flexible.Last year i started my working life and got a chance to learn symfony2. In Symfony2, every project is tight coupled in the form bundle. So the top 5 bundle that every symfony2 developer should use:

  • ¬†FOSUserBundle:-¬†this is the best bundle for those who want ¬†to manage user management in their application. A best alternate of sf guard for those people who wants to ¬†migrate their project from symfony1.4 to symfony2.
  • FOSFacebookBundle:-Integrate the Facebook Platform into your Symfony2 application.
  • SonataAdminBundle :- Admin generator
  • FOSRestBundle:-This Bundle provides various tools to rapidly develop RESTful API’s with Symfony2
  • AsseticBundle

 

How to make a twig extension in symfony2

Twig is a the flexible, fast, and secure PHP template engine.

I started  using twig since i found this in symfony2.

I was trying to use split function of php in twig but i was not able to split a text.
So i started to¬†implement twig¬†filter. In this tutorial I’m going to make a simple Twig extension.
First we have to write a twig extension.
Step 1: Create a Extension Folder in the Bundle
Step 2: Create a php file in Extension Folder called MyTwigExtension.php

Continue reading

How to modify error page in Symfony2

It is very important for a website to customize the error page i.e error 404 page.Today I will tell you how to modify error page in Symfony2. When there is a error in symfony2 an exception is generated. The framework shows a detail information about the error. But when we put are application in production environment, we will need a very friendly error message page or a sitemap page.

You need to create a page and a controller that will generate a response.In my application, we are creating a ExceptionController.

The Execption Controller looks like

<?php namespace Dev\DreamBundle\Controller;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;

class ExceptionController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
/**
* Converts an Exception to a Response.
*
* @param FlattenException $exception A FlattenException instance
* @param DebugLoggerInterface $logger A DebugLoggerInterface instance
* @param string $format The format to use for rendering (html, xml, …)
* @param Boolean $embedded Whether the rendered Response will be embedded or not
*
* @throws \InvalidArgumentException When the exception template does not exist
*/
public function exceptionAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = ‘html’, $embedded = false)
{
$arraytopass= array(‘exception’ => $exception);
return $this->render(‘DevDreamBundle:Exception:error.html.twig’,$arraytopass);
}

}
?>
The Resources/views/Exception/error.html.twig template would look like:

{% block body %}
<div>Thank you for reading this tutorial………
customized error page
</div>
{% endblock %}

For the final step you need to tell Symfony to use your controller to handle exceptions. Set the following in config_prod.yml:
Symfony/app/config/config_prod.yml

parameters:
exception_listener.controller: “DevDreamBundle:Exception:exception”

After doing all this please clear your cache(Very Important Step)
command is “app/console cache:clear”

You are good to go…….

Using multiple databases in Symfony2

Time change everything. When i was started learning php i never  heard the name  Symfony. But today i love this framework. Now i will tell you how we can configure and use  two databases in Symfony2 .

We have to do the following Steps:-

1.Open the config.yml (Symfony->app->config->config.yml)

imports:
    - { resource: parameters.ini }
    - { resource: security.yml }

framework:
    secret:          %secret%
    charset:         UTF-8
    router:          { resource: "%kernel.root_dir%/config/routing.yml" }
    form:            true
    csrf_protection: true
    validation:      { enable_annotations: true }
    templating:      { engines: ['twig'] } #assets_version: SomeVersionScheme
    session:
        default_locale: %locale%
        lifetime:       600
        auto_start:     true

# Twig Configuration
twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%
    extensions:
     - twig.extension.debug
     - twig.extension.text

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    filters:
        cssrewrite: ~
        # closure:
        #     jar: %kernel.root_dir%/java/compiler.jar
        # yui_css:
        #     jar: %kernel.root_dir%/java/yuicompressor-2.4.2.jar

# Doctrine Configuration
doctrine:
    dbal:
        default_connection:       default
        connections:
            default:
                dbname:           devdream
                user:             aman
                password:         root
                host:             192.168.2.102
            forum:
                dbname:           dev
                user:             aman
                password:         root
                host:             192.168.2.102
    orm:
        default_entity_manager:   default
        entity_managers:
            default:
                connection:       default
                mappings:
                    DevDreamBundle: ~
                metadata_cache_driver: apc
                query_cache_driver: apc
                result_cache_driver: apc
            forum:
                connection:       forum
                mappings:
                    DevDreamBundle: ~
# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%

jms_security_extra:
    secure_controllers:  true
    secure_all_services: false

i have modified  the connection part and write the names of both the databases.

This is the configuration part

2. In the second step, i will tell you , how to use the second database in controller

So this is the following code for access second database in Controller

$query=   $this->get('doctrine')->getEntityManager('forum')
                        ->createQuery("SELECT th FROM DevDreamBundle:devdreamUser th")
                        ->setMaxResults(4)
                        ->getResult();

You are good to go……