action without a view. Need a module?
I want to execute an action which doesn开发者_Go百科t really need a view. In this example this is a "logout" function, but this could be even an AJAX call (for example, I just need a number back).
Should I add a module? Well, I'd rather do a "flat" script as an action which redirect to the index page, when its done. And in this case, where to put this flat script?
Yes, you still want to create a module (controller) for this.
For a number of reasons:
- Should you want to change the behaviour, it's already in your code.
- For a logout action you want to manipulate the current (sfUser
) user, to get an instance you want to fire up the symfony stack, so why not create the controller.
- It's considered 'bad practice', or 'counter-MVC' if you start creating all kinds of small files for small actions.
And how hard is it to create a new module? ./symfony generate:module frontend user
.
If you don't have a view you can always forward/redirect the request. (in your action: $this->redirect('homepage');
).
Or, for you AJAX-actions, you can return only the required data:
if ($request->isXmlHttpRequest()) {
// set correct header
return $this->renderText(json_encode($data));
}
精彩评论