YII2 How to invoke controller action from the screen
I have a function in my controller that manipulates the data the way I wanted. Now I want to call this function on a file index.php
in view
. How to do it?
IN MY CONTROLLER
function actionTesting($params){
.....
}
How can I call it in the form like ..
<?php
echo $this->testing($params);//Calling unknown method: yii\web\View::testing()
?>
source to share
You shouldn't call controller actions from the view. I think this breaks the MVC pattern.
As for the error, it's clear what $this
in the image refers to yii\web\View
and not to the controller, and the method testing
obviously doesn't exist there.
There is a similar question asked earlier, here is a possible solution (credits to Manesh ):
Yii::$app->runAction('controller/action', ['param1' => 'value1', 'param2' => 'value2']);
It's not enough to just call the controller action like a normal method call, because some events need to be applied, etc.
I do not recommend using this approach, it is better to move your logic to component / model depending on its type.
source to share