如何在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

@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.

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.