Answer the question
In order to leave comments, you need to log in
Yii. How to use your functions in the controller?
How to write your own functions in the controller so that they can be called from any action?
In principle, I found a solution, but something tells me that it smells of bydlokoding :)
I do it this way. My function inside controller:
public static function myFunction(){
//тело функции
}
MyController::myFunction();
Answer the question
In order to leave comments, you need to log in
Something prompts to me that it is better (more correctly) to implement the methods in MODEL. This will not conflict with MVC. And already from the controller to call the methods of the model.
It can also be moved to a component.
It's better to use a model to store your functions.
And controllers should be thin.
For example, to get a user by the id of the User model, you can implement it statically:
<?php
class User extends CActiveRecord {
...
static function get($id){
return self::model()->findByPk($id);
}
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question