The main configuration file to handle routing is here app/config/routing.yml (or routing_dev.yml if you are in dev environement).
This is the default configuration:
MyHelloBundle:
resource: "@MyHelloBundle/Controller/"
type: annotation
prefix: /
This tells to Symfony to interpret the routing instructions inside the MyHelloBundle controller.
[IMPORTANT:] Anotations are orders added in comments before classes, methods and variables inside PHP classes.
In MyHelloBundle/Controller/DefaultController.php we can see:
/**
* @Route("/hello/{name}")
* @Template()
*/
So if I type this http://localhost/app_dev.php/hello/maria my browser will display:
Hello maria!
Well, if you prefer to see Hello world just type:
http://localhost/app_dev.php/hello/world
You will see a Hello world! message
Routing can be handled with different formats: YAML, XML, PHP, Annotations...
Create a home page with a welcome message
Open the controller file: MyHelloBundle/Controller/DefaultController.php
And change
/**
* @Route("/hello/{name}")
* @Template()
*/
public function indexAction($name)
{
return array('name' => $name);
}
By
/**
* @Route("/", name="homepage")
* @Template()
*/
public function indexAction()
{
return array();
}
Here we are defining the home page, so there are no paremeters for the action because is the home page.
In the home page we're going to show a Welcome message to visitors, to do so we are going to modify the view part, the template associated to our
Controller.
You can verify routing by typing this on the command line:
php app/console router:debug
Open index.html.twig dans src/MyHelloBundle/Resources/views/Default and type:
Welcome to my first Symfony 2 project !
Delete the demo bundle
Let's delete the demo bundle "Acme"
1) Delete the folder src/Acme
2) In app/AppKernel.php delete the line that loads the Acme bundle "$bundles[] = new AcmeDemoBundleAcmeDemoBundle();"
3) Delete the first 3 lines in; routing_dev.yml:
Code:
_welcome:
pattern: /
defaults: { _controller: AcmeDemoBundle:Welcome:index }
_demo_secured:
resource: "@AcmeDemoBundle/Controller/SecuredController.php"
type: annotation
_demo:
resource: "@AcmeDemoBundle/Controller/DemoController.php"
type: annotation
prefix: /demo
Browse to: http://localhost/app_dev.php and you will see: "Welcome to my first Symfony 2 project !"
It works! now you can go and get some coffee!
Create controllers in Symfony 2
Back from your coffee? Great! So let's go on with controllers.
In Symfony 2 controllers are inside the folder Controller/ in our bundles. All controllers should be named WhatevernameController.php
The default views for the controllers are in Resources/views/Whatevername/ So is important to choose a good name for your controller.
This is how to create a controller called "Front" inside the Bundle
1. Create the file: MyBundle/Controller/FrontController.php
class FrontController extends Controller
{
/**
* @Route("/front/{param}")
* @Template()
*/
public function indexAction($param)
{
return array('param' => $param);
}
}
2. Change /app/config/routing.yml to point to the new folder
3. Add the folder Resources/views/Front
4. Add the template Resources/views/Front/index.html.twig and type inside:
{{param}} world!
Check if your routing is all right:
php app/console router:debug
Now browse it: http://localhost /app_dev.php/front/Hello
Guess what you will see? Hello world! Original, mmm?
@Template specifies that the action is linked to a template. If the parentheses are empty the default template will be in MyBundle/Resources/views/Front/index.html.twig
Where index.html.twig is the action name.
Whitin an action there could be a Response in which case the template will not be used (no need for @Template annotation)
use Symfony\Component\HttpFoundation\Response;
public function indexAction($param)
{
$param=$request->query->get('param');
return new Response('Parameter: '.$param.'');
}
You can return a parameter with a specific value, with the @Template annotation, it will be:
return array('param'=>"Hello");