Cleanups in symfony 2

Before we start writing our own app, we need to do some cleanups. We need to configure AppBundle to use the YAML configuration. While this is possible to switch the configuration format manually, we will remove the bundle (it doesn’t contain much code yet), and we will generate a new bundle to demonstrate new bundle creation.

Recreating AppBundle

To recreate AppBundle, remove it first as follows:

$ rm -rf src/AppBundle

Remove it from app/AppKernel.php by removing the following line:

new AppBundleAppBundle(),

Remove it’s routing (app/config/routing.yml), which is as follows:

    resource: @AppBundle/Controller/
    type:     annotation

Now we will need to generate it again. Issue the following command:

$ php app/console generate:bundle --namespace=AppBundle

Now provide the following answers to the generator:

Bundle name [AppBundle]: [Enter]
Target directory [.../todoapp/src]: [Enter]
Configuration format (yml, xml, php, or annotation): yml
Do you want to generate the whole directory structure [no]? [Enter]
Do you confirm generation [yes]? [Enter]
Confirm automatic update of your Kernel [yes]? [Enter]
Confirm automatic update of the Routing [yes]? [Enter]

So basically, we want all the options to be default ones, and we will choose the YAML configuration to use.

After a successful generation, look into app/config/routing.yml. You will notice a slight change in this routing:

    resource: "@AppBundle/Resources/config/routing.yml"
    prefix:   /

As it does not use annotations now and routing is handled through traditional YAML files, it has some advantages, the most obvious one — you keep your AppBundle routing information in a single file rather than place it in various controllers and actions.

You will note more directories in the created bundle. As we are using the YAML format now, we also got the services.yml file within the Resources/config file, and we have the DependencyInjection directory with two files created.

There is also one more change, look into the controller. It is much shorter now:

namespace AppBundleController;

use SymfonyBundleFrameworkBundleControllerController;

class DefaultController extends Controller
    public function indexAction($name)
        return $this->render('AppBundle:Default:index.html.twig', 
               array('name' => $name));

The routing annotation is gone; action code seems much cleaner now. You may also notice a slight difference within the indexAction method. The method now gets a parameter and returns it in an array. The render argument has also been slightly changed. To explain this change in the parameters of the method, we need to dive into routing.

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest


Get the best in web dev

Join and recieve best in web dev , once a week FREE

An email has been Sent to your Inbox ! Please Confirm your Subscription :)