Posts Tagged with components

Displaying 1-3 of 3 results.
Wiki: Yii 1.1: More information to Yii::app() user - id, userrole etc.wiki
posted by admin on March 2, 2016
By default, the expression Yii::app()->user returns a CWebUser application component which represents the information that are closely related with the current user. Some information can be persistent throughout the current user session. For example, CWebUser already comes with a name property that stores the username of the current user.

In order to store more information, we need to modify the identity class used together with CWebUser. Each application may have one or several identity classes which are mainly responsible to provide ways of performing user authentication.

Here we use the UserIdentity class included in the testdrive application as an example, assuming our goal is to add the ID and the last login time of the user to CWebUser. We would modify UserIdentity as follows,


class UserIdentity extends CUserIdentity
{
private $_id;

public function authenticate()
{
$user=User::model()->findByAttributes(array('username'=>$this->username));
if($user===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($user->password!==md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$user->id;
$this->setState('userrole', $user->userrole_id);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}

public function getId()
{
return $this->_id;
}
}


In the above, during authentication we retrieve the ID and the last login time information of the authenticated user. We save the ID in a private variable $_id and save lastLoginTime in a state by calling setState(). The reason that we use different approaches to save id and lastLoginTime is because id is a pre-defined property that is recognized by CWebUser. If we want to store more information, we should use setState(), like we do with lastLoginTime.

We also override the getId() method to return the private variable $_id. The parent implementation is to return the username.

That's all we need. Now if we want to retrieve the id or lastLoginTime information in our code, we can do the following:

$id=Yii::app()->user->id;
$userrole=Yii::app()->user->userrole;
Read more
Resolved: Stop loading jquery library from assets folder in Yii
posted by admin on January 24, 2016
I had problem in my app that made it slow , after checked firebug I noted that jquery-ui loaded twice first from google.com and second from assets folder ("232kb") .

How to force it to load from google.com, or localhost, or jquery.com without assets version ?Read more
Wiki: Yii - пример создания виджета - widget example - шаблон для контроллераwiki
posted by admin on January 22, 2016
Итак - вот мой контроллер - он лежит в папке /protected/components

<?php
/* рассмотрим здесь "параметризированный" виджет*/
class AdminMenuWidget extends CWidget {

/*в общем случаем может
хранить целый массив свойст - нас же интересует
только текущее действие
- чтобы подсветить активный элемент меню*/
public $params = array(
// пусть по умолчанию будет активна ссылка на главную
'action'=>'index',
// следующие два не использую - просто для примера
'parameter 2'=>'value 2',
'parameter 3'=>'value 3',
);

public function run() {
// передаем данные в представление виджета
$this->render('adminmenu',array('params' => $this->params));
}
}


представление контроллера для этого вижжета лежит рядом - а именно так -

/protected/components/views


и выглядит так:

< ul class="nav nav-pills" >
< li<?php if ($params['action']=='index')
echo ' class="active"' ;
?>>
<?php echo CHtml::link('Админка', array('perfomance/index')); ?>
< /li >

< li<?php if ($params['action'] == 'showtickets')
echo ' class="active"' ;
?>>
<? php echo CHtml::link('Заявки', array('perfomance/showtickets')); ?>
< /li >

< li<?php if ($params['action'] == 'rebuildroles')
echo ' class="active"' ;
?>>
<?php echo CHtml::link('Роли', array('perfomance/rebuildroles')); ?> < /li >
</ul>


Как отобразить виджет
Это просто - например в представлении некоего контроллера я пишу:

<?php
$this->beginWidget('AdminMenuWidget',array(
'params'=>array(
//получаем текущее действие чтобы подсветить активный элемент меню
'action' => Yii::app()->getController()->getAction()->getId(),
)));
$this->endWidget();
?>
Read more