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

Running age/distance LINQ query on EF result query

c# linq entity-framework

I want to return a list of users from the database within a certain distance of a point and between a certain age range. Each User record in the database has three relevant fields. LastLocationLati...

Ruby on Rails, users can set their location

ruby-on-rails ruby gem location maps

So I'm developing an web application using Ruby on Rails, and I want to find a way where the user, upon creating his account, may set his location. I was thinking about something like a google maps...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.