How to do autorendering in Symfony

I'm guessing its just not done since I can't find any reference to this anywhere, even people asking without response. Although I'm hoping Symfony calls it something else.

How can I get Symfony to auto render views/{controller}/{action}.html.php

Answers


No, it's not possible in standard edition of Symfony. However, routing is just one of its components. So if you really want, you can create your own component and use it instead.


You have two options:

1) Use SensioFrameworkExtraBundle - it allows to use @Template annotation. (It's included in SE).

2) Write your own method. I found it annoying to write @Template and any annotations in controllers every time so I added this method in the base controller (it's only an example, examine before using it in production):

public function view(array $parameters = array(), Response $response = null, $extension = '')
{
    $extension = !empty($extension) ? $extension : $this->templateExtension;
    $view = ViewTemplateResolver::resolve($this->get('request')->get('_controller'), get_called_class());
    return $this->render($view . '.' . $extension, $parameters, $response);
}


class ViewTemplateResolver
{

    public static function resolve($controller, $class)
    {
        $action = preg_replace('/(.*?:|Action$)/', '', $controller);
        if (preg_match('~(\w+)\\\\(\w+Bundle).*?(\w+(?=Controller$))~', $class, $name)) {
            return implode(':', array($name[1] . $name[2], $name[3], $action));
        }
    }
}

Now in the controller we can do: return $this->view();


When you do routing with annotations instead of yaml, you can add a @Template() to your action method and it's rendering the default template as requested by you.

To do this, change your routing to annotations:

AcmeDemoBundle:
    resource: "@PAcmeDemoBundle/Controller/"
    type: annotation
    prefix: /

Within your controllers, add this for each action:

/**
 * @Route("/index", name="demo_index")
 * @Template()
 */

Actually I don't know if there is a way to get this behaviour when not using annotations. But as there seems to be logic for this, there might be one.


@meze's answers are both more desirable than out of the box behaviour.

However I think the SensioFrameworkExtraBundle he pointed me to has given me the clue that I needed to achieve this without replacing my own route.

That is to hook into the kernel view event.

Its purpose is specifically stated as:

The purpose of the event is to allow some other return value to be converted into a Response.

I'm assuming then that it can be used to convert a null return from the controller action to a response.


Need Your Help

iPhone SDK: UIButton with both image and text possible?

ios iphone xcode uibutton

I have a button of size width 200 and height 270. I want to have text and image on the same button. Not as a background image on that text. Instead of this, I want to display the text with height 1...

How to make UserControl locked to certain height?

winforms user-controls

A user is not allowed to resize a TextBox control vertically. The height of a TextBox is locked to the ideal height that a textbox should be.

Android: Storing Image into project directory (files)?

android image file directory store

I want to store my Bitmap image into project directory. How can I have access to my project folder or what is the address of my project folder?