Symfony Routing Cookbook: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
No edit summary
Line 25: Line 25:
To map the `indexAction` controller to the `Resources/views/index.html.twig` template:
To map the `indexAction` controller to the `Resources/views/index.html.twig` template:


<syntaxhighlight lang="php" highlight="7,11">
<syntaxhighlight lang="php" highlight="4,10,13">
// MyBundle/Controller/MyController.php
// MyBundle/Controller/MyController.php
// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;


class MyController extends Controller
class MyController extends Controller

Revision as of 15:50, 29 January 2015

Routing aliases

# app/config/routing.yml

# redirecting the root
# a route to "/app" must be defined somewhere...
root:
    path: /
    defaults:
        _controller: FrameworkBundle:Redirect:urlRedirect
        path: /app
        permanent: true

Routing to a template with the same name as the action

Use the @Template annotation[1], which is part of the SensioFrameworkExtraBundle.[2]

This is for cases where the template names match the controller names.

In this case the controllers return arrays containing variable page content, no Response objects. If they return Response objects the @Template annotation will be ignored.

To map the indexAction controller to the Resources/views/index.html.twig template:

// MyBundle/Controller/MyController.php

// ...
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class MyController extends Controller
{
	/**
	 * @Route("/", name="_my_landing")
	 * @Template()
	 */
	public function indexAction()
	{
		return array(
			'foo' => 'bar',
			'biz' => 'bash');
	}
}

Notes