2
votes

I have a public function in model Yii:

public function test() {
    echo 'I am working';
}

Now I want to call this function on view Yii. I wolud like to go through via controller instead of directly calling that function on view Yii.

So how can I call the function on view Yii? and what I have to do with controller before I call it on view Yii?

2
Your controller should just send whatever needs to be presented to the view aka the output of your function. This is the way MVC works. You have to make sure not to mix your presentation with your business logic.Alternatex
you can call it the same as a normal model so Model::model()->test();chaos505

2 Answers

6
votes

I prefer to put it on your controller, like this

$model_result = MyModel::model()->test();
$this->render('view', array('model_result' => $model_result));

On your view.php

<div class="my_class" >
    <?php echo $model_result;?>
</div>
0
votes

View should not call any functions (unless perhaps formatting helper functions), controller should pass to the view whatever values it needs, including but not limited to a result of function/method call.