Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

action without a view. Need a module?

I want to execute an action which doesnt 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?

like image 976
deeped Avatar asked Aug 25 '26 17:08

deeped


1 Answers

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));
}
like image 104
Grad van Horck Avatar answered Aug 28 '26 08:08

Grad van Horck