Domanda

I would like to change the default behaviour of the @Template annotation which automatically renders the template named as the controller action.

So in an ArticleController.php

/**
 * @Route("/new", name="article_new")
 * @Method("GET")
 * @Template()
 */
public function newAction()
{
    // ...
    return array();
}

would render Article/new.html.twig.

I want to change this to referr to the name of the route the action was called with so you could have multiple routes for an action each rendering a different template.

This is the way I currently do it (without @Template):

/**
 * @Route("/new", name="article_new")
 * @Route("/new_ajax", name="article_new_ajax")
 * @Method("GET")
 */
public function newAction()
{
    // ...
    $request = $this->getRequest();
    $route = $request->attributes->get('_route');
    $template = 'AcmeDemoBundle:' . $route . '.html.twig';

    return $this->render($template, array(
        // ...
    ));
}

I wonder now if there is a way to change the behaviour of @Template to do exactly that. Is there a way to customize the annotations or just some aproach to make it more automated? Any ideas?

È stato utile?

Soluzione

I have now found a solution using the kernelView event. This is independet of the @Template annotation. The kernelView event fires whenever a controller action doesn't return a response object.

(This solution is based on Symfony 2.4)

event listener service:

services:
    kernel.listener.route_view:
        class: Acme\DemoBundle\Templating\RouteView
        arguments: ["@request_stack", "@templating"]
        tags:
            - { name: kernel.event_listener, event: kernel.view }

event listener class:

namespace Acme\DemoBundle\Templating;

use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

class RouteView
{
    protected $controller;
    protected $route;
    protected $templating;

    function __construct(RequestStack $requestStack, $templating)
    {
        $this->controller = $requestStack->getCurrentRequest()->attributes->get('_controller');
        $this->route      = $requestStack->getCurrentRequest()->attributes->get('_route');
        $this->templating = $templating;
    }

    public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $controllerAction = substr($this->controller, strrpos($this->controller, '\\') + 1);
        $controller = str_replace('Controller', '', substr($controllerAction, 0, strpos($controllerAction, '::')));
        $template = 'AcmeDemoBundle:' . $controller . ':' . str_replace(strtolower($controller) . '_', '', $this->route) . '.html.twig';

        $response = $this->templating->renderResponse($template, $event->getControllerResult());

        $event->setResponse($response);
    }
}

Now the controller behaves like this:

/**
 * @Route("/new", name="article_new")           -> Article:new.html.twig
 * @Route("/new_ajax", name="article_new_ajax") -> Article:new_ajax.html.twig
 * @Method("GET")
 */
public function newAction()
{
    // ...

    return array();
}

Altri suggerimenti

FOSRestBundle includes similar functionality to @Template but on class-level since my pull request if you use the @View annotation on class-level.

This can be useful if want to your template-filenames to reflect the action-names but not the route-names ( as opposed to what was asked for in the question ).

The rendered template will be i.e. ...

<controller-name>/<action-name>.html.twig

... for HTML views.

Example: AcmeBundle\Controller\PersonController::create() will render

 AcmeBundle/Resources/views/Person/create.html.twig 

Before the PR you had to annotate every method.

Annotating a method still gives the possibility to override template,template-variable and status-code though.

example:

/**
 * @FOSRest\View(templateVar="testdata", statusCode=201)
 */
class PersonController implements ClassResourceInterface
{
    public function newAction()
    {
        return $this->formHandler->createForm();

        // template: Person/new.html.twig 
        // template variable is 'form'
        // http status: 201
    }

    public function helloAction()
    {
        return "hello";

        // template: Person/hello.html.twig
        // template variable 'testdata' 
        // http status: 201
    }

    /**
     * @FOSRest\View("AnotherBundle:Person:get", templatevar="person")
     */
    public function getAction(Person $person)
    {
        return $person;

        // template: AnotherBundle:Person:get 
        // template variable is 'person' 
        // http status: 201
    }

    /**
     * @FOSRest\View("AnotherBundle:Person:overview", templatevar="persons", statusCode=200)
     */
    public function cgetAction()
    {
        return $this->personManager->findAll();

        // template: AnotherBundle:Person:overview 
        // template variable is 'persons'
        // http status: 200
    }

    // ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top