January 06, 2013

# Create a Web Applications with Symfony2 Framework Part 2 - Bundles and routing

9:00 am
2
In my previous article, I have explained about How to create a web application with Symfony 2 framework part 1. In this article, I'm going to explain about, How to bundle and routing with Symfony2 Framework.

If you have any doubt in installing and configuring the Symfony2 framework, Check my previous article "Installing and Configuring Symfony2 framework on Linux Ubuntu OS". If you need more details about, How to create a web application with Symfony2 framework, Check the article "Create a Web application with Symfony2 Framework - Part 1".

Step 1 : Create Bundle

At the beginning, Symfony 2 Framework makes sense that the main bundle "TestSymfonyBundle" create. Go to the cmd.exe the path to the root directory of the web application with php app/console it calls help. There you can read about the various basic commands and become better acquainted with the Symfony application consoles familiar.

With the following command we can create our example project finally the bundle : phpapp/console init: bundle Test\SymfonyBundle src/

* init: bundle is the command to initialize the bundle
* Test \ SymfonyBundle is the PHP 5.3 namespace
* src / corresponds to the folder in which the bundle is to be created

If everything goes well, an application of the consoles Symfony2 Framework already suggests the next steps. Additionally, you can look in the editor's choice created DefaultControllerFactory under / src / Test / SymfonyBundle / controller.

Step 2 : Bundle activate and integrate into the application

Symfony2 Framework is very economical in "guessing" of components or plugins, as this behavior costs a lot of performance and programmer unnecessarily squeezed into a programming style. Therefore, the developer must active the bundle tell the autoloader.

For this we open the first AppKernel (/app/AppKernel.php) and adds the newly created SymfonyBundle in the Function register bundles () added.
To do this simply extends the variable $bundles by the following lines: //Example Scandio new Test\SymfonyBundle\TestSymfonyBundle (), We also can use the namespace makes sense, we have to register it in the autoloader. Nothing easier than that: /// App/autoload.php$Loader -> register namespaces(array(
...
/ / Symfony
'Symfony' => __ DIR__. '/.. / src'
));

For our example, these settings are sufficient. As can be seen in the variety of configurations allow the developer Symfony2 very much freedom to design their applications as they wish.

Step 3 : Configuring Routing

An important component of Symfony2 is the routing. Routing is finding the way here the URL to the appropriate controller action. Unlike, for example CakePHP routes here are not automatically created (these are mostly based on conventions), but the developer the way is treated as a route before.

This turns - with the necessary programming discipline - to be very advantageous. As a developer, you can customize the URLs here with his wishes, without the risk of violating coding standards, and so produce unnecessary mistakes.

As already described in the first part of this series, we need for our application six basic routes ( index, add, edit, delete, markAsDone, markAsUndone ). The Referenzauf the routes are stored in the /app/config/routing.yml file. If that is not common or YML format to another format (ex.XML) is preferred, this can also use in Symfony2 Framework. The framework allows the programmer a lot of liberties here, as long as it is configured.

Based on the sample routes you can already see the Best Practices: The relevant route information should be stored in the bundle, in order to facilitate a transfer later. The following must be entered in the routing.yml so that the routes are detected by the application: #/app/config/routing.yml # Test Symfony2 tutorial test_symfony_tutorial: resource: "@TestSymfonyBundle/Resources/config/routing.yml"

Next the routes are entered into the currently referenced routing config. For this, I recommend reading the excellent again declared Symfony2 online manuals . There is explained clearly and in detail, what routes are and how they are created.

Maybe it will surprise some readers that, add/edit action in two different controller functions have been split. The point is that through this code is readable and understandable better. Additionally, one can wiedervewenden so features or actions directly. #index: Show all tasks ordered by priority test_symfony_index: pattern:/symfony defaults: {_controller: TestSymfonyBundle:Task:index}

test_symfony_view:
requirements:

requirements:
_method: get

requirements:
_method: post

# Edit: Show form with prevoius task data
test_symfony_edit_prepare:
requirements:
_method: get

# Edit: Process Form Data to edit task
: test_symfony_edit_prepare
pattern: / symfony / edit / {taskId}
requirements:
_method: post

# Delete: deletes an existing task
test_symfony_delete:
requirements:

# MarkAsDone: Mark an existing task as done
test_symfony_markAsDone:
requirements:

# MarkAsDone: Mark an existing task as undone
test_symfony_markAsUndone:
requirements:

If all routes were added successfully, we can test via console whether the required routes are recognized by Symfony2. The following output appears on successful route configuration: