Reputation: 535
I am developing a social networking website using Yii. While frequently using the following things I am having great data manageability issue. - User ID - Current user ID (the user which profile is the owner viewing) - Is owner???
where can I define these things. I would something like
if(Yii::app()->owner==ME){
//do something
}
// and similarly
if($this->isMyFreind(<Current user ID>){
}
// $this(CanIView()){
}
I want these functions to be public for any page? But how? In other words Where can I put my library which contains my own favorite functions like text shortening, image cropping, date time format etc etc??
Upvotes: 0
Views: 4216
Reputation: 2623
In Yii, you can do achieve this by making a class (under protected/compoents) which inherits
CApplicationComponent
class. And then you call any property of this class globally as a component.
class GlobalDef extends CApplicationComponent {
public $aglobalvar;
}
Define this class in main config under components as:
'globaldef' => array('class' => 'application.components.GlobalDef '),
And you can call like this:
echo Yii::app()->globaldef->aglobalvar;
Hope that can help.
Upvotes: 3
Reputation: 2198
According to the MVC model, things like image cropping or date time formats would go in models. You would simply create models for that.
Upvotes: 1
Reputation: 5913
I usually use a globals.php file with all my common functions In the index.php (yip the one in the root):
$globals='protected/globals.php';
require_once($globals);
I can then call my global functions anywhere. For example, I shorten certain Yii functions like:
function bu($url=null){
static $baseUrl;
if ($baseUrl===null)
$baseUrl=Yii::app()->request->baseUrl;
return $url===null ? $baseUrl : $baseUrl.'/'.ltrim($url,'/');
}
So I can then call the Yii::app()->request->baseUrl
by simply calling bu()
Upvotes: 0