If a piece of code needs to be executed in multiple controller actions it should be factored out and moved into a new class. Depending on the actual logic this new class can be an action filter/controller behavior, an application component or a simple static helper. This way you can avoid unnecessary coupling between controllers and don’t have to (ab)use the framework’s routing mechanism for a simple method call.
i think sometimes, is necessary, call actions from one controller to another, and it is the latter that is responsible for the response.
for exanple:
if after an action in the controller ‘a’ where I get a model car in the $ _GET and after certain operations want to show the view of the car it would make sense to call from the controller ‘a’ to CarController->actionView() to show the car
Thank you all for your answers. I used another approach to tackle this issue.
First, I created an Utils Class in Assets folder:
namespace backend\assets;
use Yii;
class Utils
{
public function myFunction($param1, $param2)
{
// do calculations
}
}
Then I instantiated the Utils Class to execute the desired function.
public function actionLogout()
{
$utils = new \backend\assets\Utils;
$utils->myFunction('007', 1);
Yii::$app->user->logout();
return $this->goHome();
}