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}
# View: Display a task
test_symfony_view:
pattern: /symfony/view/{taskId}
defaults: {_controller: TestSymfonyBundle: Task: view}
requirements:
taskId: \ d +
# Add: ShowForm
test_symfony_add_prepare:
pattern: /symfony/add
defaults: {_controller: TestSymfonyBundle:Task:addPrepare}
requirements:
_method: get
# Add: Process Form Data to add new task
: test_symfony_add_prepare
pattern: /symfony/add
defaults: {_controller: TestSymfonyBundle:Task:addProcess}
requirements:
_method: post
# Edit: Show form with prevoius task data
test_symfony_edit_prepare:
pattern: /symfony/edit/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:editPrepare}
requirements:
_method: get
taskId: \ d +
# Edit: Process Form Data to edit task
: test_symfony_edit_prepare
pattern: / symfony / edit / {taskId}
defaults: {_controller: TestSymfonyBundle:Task:editProcess}
requirements:
_method: post
taskId: \ d +
# Delete: deletes an existing task
test_symfony_delete:
pattern: /symfony/delete/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:delete}
requirements:
taskId: \ d +
# MarkAsDone: Mark an existing task as done
test_symfony_markAsDone:
pattern: /symfony/done/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:markAsDone}
requirements:
taskId: \ d +
# MarkAsDone: Mark an existing task as undone
test_symfony_markAsUndone:
pattern: /symfony/undone/{taskId}
defaults: {_controller: TestSymfonyBundle: Task: markAsUndone}
requirements:
taskId: \ d +
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:
C:\path\to\project> php app/console router: debug [router] Current routes Name Method Pattern _wdt ANY/_wdt/{token}_profiler_search ANY/_profiler/search _profiler_purge ANY/_profiler / purge _profiler_import ANY / _profiler / import ANY / _profiler / export / {token}. txt _profiler_export _profiler_search_results ANY / _profiler / {token} / search / results _profiler ANY / _profiler / {token} _configurator_home ANY / _configurator / _configurator_step ANY / _configurator / step / {index} _configurator_final ANY / _configurator / final _welcome ANY / _demo_login ANY / demo / secured / login _security_check ANY / demo / secured / login_check _demo_logout ANY / demo / secured / logout acme_demo_secured_hello ANY / demo / secured / hello _demo_secured_hello ANY / demo / secured / hello / {name} _demo_secured_hello_admin ANY / demo / secured / hello / admin / {name} _demo ANY / demo / _demo_hello ANY / demo / hello / {name} _demo_contact ANY / demo / contact test_symfony_index ANY / symfony test_symfony_view ANY / symfony / view / {taskId} test_symfony_add_prepare POST / symfony / add test_symfony_edit_prepare POST / symfony / edit / {taskId} test_symfony_delete ANY / symfony / delete / {taskId} test_symfony_markAsDone ANY / symfony / done / {taskId} test_symfony_markAsUndone ANY / symfony / undone / { taskId}
As you can see, our previously entered routes are available in the application. To those now filled with "life", we will create in the next part of the task controller that handles the processing and presentation.
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}
# View: Display a task
test_symfony_view:
pattern: /symfony/view/{taskId}
defaults: {_controller: TestSymfonyBundle: Task: view}
requirements:
taskId: \ d +
# Add: ShowForm
test_symfony_add_prepare:
pattern: /symfony/add
defaults: {_controller: TestSymfonyBundle:Task:addPrepare}
requirements:
_method: get
# Add: Process Form Data to add new task
: test_symfony_add_prepare
pattern: /symfony/add
defaults: {_controller: TestSymfonyBundle:Task:addProcess}
requirements:
_method: post
# Edit: Show form with prevoius task data
test_symfony_edit_prepare:
pattern: /symfony/edit/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:editPrepare}
requirements:
_method: get
taskId: \ d +
# Edit: Process Form Data to edit task
: test_symfony_edit_prepare
pattern: / symfony / edit / {taskId}
defaults: {_controller: TestSymfonyBundle:Task:editProcess}
requirements:
_method: post
taskId: \ d +
# Delete: deletes an existing task
test_symfony_delete:
pattern: /symfony/delete/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:delete}
requirements:
taskId: \ d +
# MarkAsDone: Mark an existing task as done
test_symfony_markAsDone:
pattern: /symfony/done/{taskId}
defaults: {_controller: TestSymfonyBundle:Task:markAsDone}
requirements:
taskId: \ d +
# MarkAsDone: Mark an existing task as undone
test_symfony_markAsUndone:
pattern: /symfony/undone/{taskId}
defaults: {_controller: TestSymfonyBundle: Task: markAsUndone}
requirements:
taskId: \ d +
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:
C:\path\to\project> php app/console router: debug [router] Current routes Name Method Pattern _wdt ANY/_wdt/{token}_profiler_search ANY/_profiler/search _profiler_purge ANY/_profiler / purge _profiler_import ANY / _profiler / import ANY / _profiler / export / {token}. txt _profiler_export _profiler_search_results ANY / _profiler / {token} / search / results _profiler ANY / _profiler / {token} _configurator_home ANY / _configurator / _configurator_step ANY / _configurator / step / {index} _configurator_final ANY / _configurator / final _welcome ANY / _demo_login ANY / demo / secured / login _security_check ANY / demo / secured / login_check _demo_logout ANY / demo / secured / logout acme_demo_secured_hello ANY / demo / secured / hello _demo_secured_hello ANY / demo / secured / hello / {name} _demo_secured_hello_admin ANY / demo / secured / hello / admin / {name} _demo ANY / demo / _demo_hello ANY / demo / hello / {name} _demo_contact ANY / demo / contact test_symfony_index ANY / symfony test_symfony_view ANY / symfony / view / {taskId} test_symfony_add_prepare POST / symfony / add test_symfony_edit_prepare POST / symfony / edit / {taskId} test_symfony_delete ANY / symfony / delete / {taskId} test_symfony_markAsDone ANY / symfony / done / {taskId} test_symfony_markAsUndone ANY / symfony / undone / { taskId}
As you can see, our previously entered routes are available in the application. To those now filled with "life", we will create in the next part of the task controller that handles the processing and presentation.
Nice web applications with symfony.
ReplyDeletePHP Development Company
thanks for sharing this best web design company
ReplyDelete