On Wed, Oct 27, 2010 at 12:23 AM, Lukas Kahwe Smith <[email protected]> wrote:
> Yet another approach is going with the Agavi approach of having a "real" view 
> layer inbetween the controller and the templates.

We discussed this to death already so I won't go over it again, but to
me this is the only sane approach. The rest will just be dirty hacks.
I don't want to go all the way to the Agavi model and have mandatory
view classes though, but if you could have a default view class that
handles json/xml/blah in a relatively sane way. Then you could have an
optional override of that class when needed if you need custom logic.

That will however require some heavy changes in the way actions are
processed imo.

In order to inject that extra view layer in between, you need the
action to give you two things: data, and a view class that will render
it. At the moment actions just return a response, this won't work
obviously. What could work is this for example:

public function viewPostAction($postId) {
    // fetch post by id.. yadda yadda
    $this->setData($post);
    // optionally return a view, as an instance or a service name (?)
    return "my_view_service";
}

Then the view has a bunch of methods that render html/json/xml/pdf.. I
would say the framework should inject the data and the request at
least into the view via some interface.

If you don't return a view, then the default sf view would just take
the action name and say viewPostAction maps to the viewPost.twig
template for html, or maybe you can have a $this->setTemplate() thingy
that would just go and override the default template mapping for the
current action, for json it'd just do a plain json_encode of the
entire data, etc.

When needed, if you need to massage your json data for example, but
not the rest, then you can extend and override that method and do what
needs to be done. Views should be services imo so that you can inject
everything as you need.

To go back to lukas' example, to return an html form inside a json
body you'd just, in the handleJson() method, do the following, which
is much easier than the crazy hackery we go through atm imo:

$form = $this['templating']->render("My:Form:template.twig", $this->getData());
$response = $this["response"]->setContent(json_encode(array('form' =>
$form, 'extraStuff' => true));
return $response;

As always, if people are interested, I can work on the implementation,
but I don't want to spend hours on this just for it to be trashed in
the end. So please speak up if you think it's good, or could be
improved, or just plain dumb.

Cheers

-- 
Jordi Boggiano
@seldaek :: http://seld.be/

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to