hi guys
i saw some one use app() instead of Yii::$app
How is this possible? and how to use it
hi guys
i saw some one use app() instead of Yii::$app
How is this possible? and how to use it
Yii::app() is for Yii1 and Yii::$app is for Yii2.
Yes I know but i saw app()->user->id in yii2
Yii::$app convert to app()
its likely that app() function is defined by user to wrap Yii::$app which in return enable the following app()->user->id in yii2, where did you see it in craftcms?
It is just a shortcut. I use lot of such procedural functions. Here is couple examples:
function set_flash($value, $key = 'success') {
Yii::$app->session->setFlash($key, $value);
}
set_flash('cool');
function can($permissionName, $params = [], $allowCaching = true) {
return Yii::$app->user->can($permissionName, $params, $allowCaching);
}
if(can('admin') && !is_role('moderator')) {
}
function assign_role($user_id, $role) {
$auth = \Yii::$app->authManager;
$authorRole = $auth->getRole($role);
$auth->assign($authorRole, $user_id);
}
assign_role('admin');
function is_guest() {
return Yii::$app->user->isGuest;
}
if(is_guest()){
Yii::$app->user->loginRequired();
}
This functions included in index.php file so they can be accessed in whole project.
But app() instead of Yii::$app looks like for very lazy guy )