Thursday 19 March 2015

"Hello World" in Symfony 2.0

This tutorial is about Symfony 2.and is based on that of  Alexandre Bacco at http://uploads.siteduzero.com/pdf/517569-developpez-votre-site-web-avec-le-framework-symfony2.pdf .

The main difference with Symfony version 1 is about the architecture with the bundles (see http://michelsalib.com/2011/07/10/a-symfony1-4-to-symfony2-migration-why-you-should-learn-symfony2/)

In Symfony 2, at first, you will have to generate a bundle executingin MS DOS prompt (after adding the PHP folder in your PATH environment variable) the following command :

php app/console generate:bundle  (we need to be within your Symfony web root)

A namespace of the bundle is then required : give it the following a name with a"Bundle" suffix -for example : [yoursite]\BlogBundle for a Blog (if you wish to make different website)

For the bundle name, it will provide you a default name in brackets, if you agree, you just have to type ENTER

Idem for the target directory

For the configuration format, I personally prefer the YML format : write "yml" and type ENTER

To the question generate the whole directory structure, say "yes" and type ENTER

And type ENTER for the following question


The bundle is now generated.



Take now your favorite IDE and open your symfony project (personally, I used Sublime Text (see http://www.sublimetext.com/) ; as Symfony is MVC framework, we will define a route then  make a controller with a view :

1 - Go to src/[yoursite]/BlogBundle\ressources\config\routing.yml and add at the end of the file the following code to insert the route of your web site:

[yoursite]_blog_helloWorld:
    path:     /hello-world
    defaults: { _controller: [yoursite]BlogBundle:Blog:index}

Bundle called "BlogBundle", Controller being "Blog" and the action being "index"
2- Add the corresponding controller in a new file called :  src/[yoursite]/BlogBundle\ressources\Controller\BlogController.php with the following code :

<?php
namespace [yoursite]\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class BlogController extends Controller
{
public function indexAction()
{
// return new Response("Hello World !");
return $this->render('[yoursite]BlogBundle:Blog:index.html.twig');
}
}
?>
3- Add the associated view in a new file  src/[yoursite]/BlogBundle\ressources\views\Blog\index.html.twig

4- In this file add the following content :
<!DOCTYPE html>
<html>
   <head>
   <title>Hello!</title>
    </head>
    <body>
          <h1>Hello World !</h1>
    </body>
</html>

5- Go to the following URL : http://localhost/symfony/web/app_dev.php/hello-world ; then, you should get this kind of screen

Adding a parameter in the controller : that means making some slightly changes to the route, the controller and the view :
  • First thing to change is the route to :

[yoursite]_blog_helloWorld:
    path:     /greeting/{name}
    defaults: { _controller: SdzBlogBundle:Blog:index}
  • The controller needs to be updated  :

[..]
class BlogController extends Controller
{
public function indexAction($name)
{
return $this->render('[yoursite]BlogBundle:Blog:index.html.twig', array('name' => $name));
}
}
  • The view needs also to be updated  :  

<!DOCTYPE html>
<html>
   <head>
   <title>Hello!</title>
    </head> 
    <body>
          <h1>Hello {{name}} !</h1>
    </body>
</html>


The URL call will then be "http://localhost/symfony/web/app_dev.php/greeting/john":