Symfony Routing Cookbook: Difference between revisions
Jump to navigation
Jump to search
| Line 36: | Line 36: | ||
== Routing aliases == | == Routing aliases == | ||
<span class="color:red;">This works, but not sure if it's the best solution.</span> | <span class="color:red;">This works, but not sure if it's the best solution.</span><ref>[http://symfony.com/doc/current/cookbook/routing/redirect_in_config.html How to Configure a Redirect without a custom Controller], Symfony documentation</ref> | ||
<syntaxhighlight lang="yaml"> | <syntaxhighlight lang="yaml"> | ||
Latest revision as of 18:17, 30 January 2015
Defining a route[edit]
Defining a route using annotations in a controller class:
// src/AppBundle/Controller/BlogController.php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
class BlogController extends Controller
{
/**
* @Route("/blog/{slug}")
*/
public function showAction($slug)
{
// ...
}
}
This basically creates a new page on the site.
Routes can alternatively be defined using Yaml, XML, or PHP, e.g.:
# app/config/config.yml
framework:
# ...
router: { resource: "%kernel.root_dir%/config/routing.yml" }
Routing aliases[edit]
This works, but not sure if it's the best solution.[1]
# 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[edit]
Use the @Template annotation[2], which is part of the SensioFrameworkExtraBundle.[3]
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');
}
}