Creating Pages in Symfony2

Creating a new page in Symfony2 is a simple two-step process :
Create a route :
A route defines the URL (e.g. /about) to your page and specifies a controller (which is a PHP function) that Symfony2 should execute when the URL of an incoming request matches the route path.
Create a controller :
A controller is a PHP function that takes the incoming request and transforms it into the Symfony2 Response object that's returned to the user.

Start by building "Hello World!" application. When you're finished, the user will be able to get a personal greeting (e.g. "Hello Symfony") by going to the following URL:

http://localhost/app_dev.php/hello/Symfony
Create a bundle
Before you begin, you'll need to create a bundle. In Symfony2, a bundle is like a plugin, except that all of the code in your application will live inside a bundle.

To create a bundle called HelloBundle ,run the following command :

$ php app/console generate:bundle --namespace=Application/HelloBundle --format=yml

Behind the scenes, a directory is created for the bundle at src/Application/HelloBundle. A line is also automatically added to the app/AppKernel.php file so that the bundle is registered with the kernel:
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
...,
new Acme\HelloBundle\AcmeHelloBundle(),
);
// ...
return $bundles;
}
Step 1: Create the Route
By default, the routing configuration file in a Symfony2 application is located at app/config/ routing.yml. Like all configuration in Symfony2, you can also choose to use XML or PHP out of the box to configure routes.
# app/config/routing.yml
acme_hello:
resource: "@HelloBundle/Resources/config/routing.yml"
prefix: /
It tells Symfony to load routing configuration from the Resources/config/ routing.yml file that lives inside the AcmeHelloBundle. This means that you place routing configuration directly in app/config/routing.yml or organize your routes throughout your application, and import them from here.

routing.yml file from the bundle is being imported, add the new route that defines the URL of the page that you're about to create:
# src/Application/HelloBundle/Resources/config/routing.yml
hello:
path: /hello/{name}
defaults: { _controller: AcmeHelloBundle:Hello:index }
Step 2: Create the Controller
When a URL such as /hello/ABC is handled by the application, the hello route is matched and the HelloBundle:Hello:index controller is executed by the framework. The second step of the pagecreation process is to create that controller.

The controller - HelloBundle:Hello:index is the logical name of the controller, and it maps to the indexAction method of a PHP class called Application\HelloBundle\Controller\HelloController. Start by creating this file inside your HelloBundle:
// src/Application/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
class HelloController
{
}
In reality, the controller is nothing more than a PHP method that you create and Symfony executes. This is where your code uses information from the request to build and prepare the resource being requested. Except in some advanced cases, the end product of a controller is always the same: a Symfony2 Response object.

Create the indexAction method that Symfony will execute when the hello route is matched:
// src/Acme/HelloBundle/Controller/HelloController.php
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'</body></html>');
}
}
The controller is simple: it creates a new Response object, whose first argument is the content that should be used in the response.

After creating only a route and a controller, you already have a fully-functional page! If you've setup everything correctly, your application should : 1 http://localhost/app_dev.php/hello/ABC

Optional Step 3: Create the Template
Templates allow you to move all of the presentation (e.g. HTML code) into a separate file and reuse different portions of the page layout. Instead of writing the HTML inside the controller, render a template instead:
// src/Application/HelloBundle/Controller/HelloController.php
namespace Application\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
return $this->render(
'AcmeHelloBundle:Hello:index.php.twig',
array('name' => $name)
);
// render a PHP template instead
// return $this->render(
// 'AcmeHelloBundle:Hello:index.php.php',
// array('name' => $name)
// );
}
}