How to create pages in Symfony2

In the last tutorials I have spoken about Symfony2 installation, controllers and bundles. Now I want explain to you how you can easily create you first page in Symfony2.6. It's very simple.

First of all, add this code

path: /random/{limit}
defaults: { _controller: AcmeDemoBundle:Random:index }

in symfony2.6\src\(Name of your bundle)\Resources\config\routing.yml.

Then, add this code

public function indexAction($limit)
return new Response('Number: '.rand(1, $limit).'');

in your Random controller(_controller: AcmeDemoBundle:Random:index). You can also change the name of your controller.

Also, you must add this namespace: use Symfony\Component\HttpFoundation\Response; in your controller.

Next go to http://localhost/app_dev.php/random/10

You will see Number: and a random number.

In this way you can create your pages in Symfony2.

Video tutorial:


Leave a Comment