Firouziam
Firouziam

Reputation: 805

symfony - calling an action from another action

i have two different actions in two different modules, and i need to execute one of them inside the other.

consider the following actions:

//first module
class module1Actions extends sfActions {

     protected function function1Form(sfWebRequest $request, sfForm $form) {
       //i need to call a function from module2 in here to do sth but i don't need any view to be returned.
       //but i need some variables to be returned by the second action
     }
}

//second module
class module2Actions extends sfActions {

     protected function executeSth(sfWebRequest $request, sfForm $form) {
       //Do Something in here and return some values.
     }
}

as i said i need to execute an action and get some values from it but i don't know how to call it. it's in a different module . can i access it ? how? and how i get the values back?

thanks.

Upvotes: 1

Views: 4318

Answers (2)

Omn
Omn

Reputation: 3070

I'd add a third method for doing this to Maerlyn's answer. Its useful in a couple of limited situations to prevent duplicating code, but to also not break a proper MVC design by moving the wrong code the the model layer.

  • Store the values to be sent/and/or returned in the myUser class then use the $this->forward() call to run the relevant action without causing the browser to make another request.

Note that $this->forward() immediately halts the action you call it from and the template for the forwarded action will be the one displayed.

Upvotes: 0

Maerlyn
Maerlyn

Reputation: 34107

Depending on how you get those values, these are the best paths:

  1. If you get them via operations on a record or a table, move the code into the model layer, and call that from both your actions

  2. If you get them some other way (like processing user input), create a utility class in your lib folder, and add it there as function, and call that from both your actions

This way you prevent code duplication, and still don't break the MVC separation of concerns.

Upvotes: 2

Related Questions