Create your first route | Symfony 2 | Beginners guide

Hey Symfony 2 lovers,

Let’s have a good understanding about routings in Symfony 2. Routing is the navigational paths (directives) in your application. For instance if you want to show all your products in your shopping cart application, which was developed by Symfony 2, routing could be either ‘your_cart/web/app_dev.php/show_all_items’ or ‘your_cart/web/app.php/show_all_items’. In here ‘your_cart‘ is the name of your application, ‘web‘ is the default path in Symfony 2 (you can hide this later), ‘app_dev.php‘ and ‘app.php‘ are two environments in the same application, such as development and production environment. These environments are there to distinguish your application in different purposes which I am not going to discuss any further. Our main concern in here is the ‘show_all_items‘ bit. This is a routing which fires a function in a controller. If you are new to a frameworks this concept might be little surprising to you. This is how the major frameworks work. When you hit a path, such as ‘show_users‘ will call a predefined function in a predefined controller. If you see something like this ‘show_users/10‘, probably that is to show how the user which has the id of 10, something like that. This 10, will be passed to a method as a parameter, such as ‘public function showUser($id)’.
Continue reading

Share