Way to call controller method in view Yii2

Is there any way to call controller method in view Yii2 like Yii1 ?

Thanks,

Absar

Not sure if it is good practise… (still a beginner in Yii :P )

But it works like following:

Examples:

in app/controllers/SiteController.php




    public function helloWorld(){

        echo "hello world";

    }



in app/views/site/anyViewFile.php




echo $this->context->helloWorld(); 



Or in any other view file:




use app\controllers\SiteController; 


echo SiteController::helloWorld(); 



Regards

1 Like

Thanks, it works for me

That’s actually a bad practice, if you need logic in the view, use a ViewHelper