# Call a Function from view

(xpectro) #1

Hello, i have a php file(MyHelpers) located in components folder,
inside of this i have the function findComandos()

i just need to call the function from a view

thanks for the help

(Alexander Makarov) #2

What exatly is the problem?

(Fabrizio Caldarelli) #3

If MyHelpers is a simple class and not a registered component in main/config.php, inside your view:

$myHelpers = new \yii\components\MyHelpers();$myHelpers->findCommandos($con);  Take care to change namespace, because \yii\components namespace suggests something related to Yii official components. (xpectro) #4 sry i will to try to explain me better example: i have my function findVar(var) located in SiteController. i need to call that function from several views for example user/index thank you for your help and for your patience (xpectro) #5 hello fabrizio thanks for your help example: i have my function findVar(var) located in SiteController. i need to call that function from several views for example user/index thank you for your help and for your patience (xpectro) #6 (xpectro) #7 (xpectro) #8 this is layouts\left here i need to call several times the function findvar($var)

actually i am copying the public function in every controller, for example

in usuarios controller
in dependencias controller etc in every controller

i want to call the function severall times but only from SiteCotroller

thanks for your time

(xpectro) #9

(Mehdi Achour) #10

Calling \backend\controllers\SiteController::findVar() should do what you want.

You may want to create a helper class and put that method in it since it has nothing to do with your controllers logic.