somebody tell how do i get a userid of logged in user.
i’ve already try to a code to get a userid in UserIdentity class…but when i try to get user ids with Yii:app()->user->getId()
it didn’t return me appropriate result.
and one more thing i am also trying to redirect user on different page after login rather than index page of site.can some body tell me to how do i change return url after login.
because i want to redirect user to his/her profile after login process.so any body give me some guidance that how could i complete this work.
hey…but anybody of you guys cal explain me how could i create a user profile of registered usr…coz i ma not using yii-user module for my project…and trying to write my own code…!!!
i want to redirect a user to his profile page after login…so would you please suggest me something about it…
i have an action with name actionProfile…but i am not getting how could i pass a user id in this function so i can get info about this particular user and show this info on view file…
i have gone through user module’s user controller and try to check some piece of code for action profile…but i was stuck when i saw a code of loadmodule action…
No there’s no need to create two files whenever there’s a need to display a single record (I actually don’t know where did that came from, there must be a misunderstanding somewhere).
If you want to write your own code, it is essential that you have the basic understanding of how Yii works. But if I may suggest, since you don’t want to make use of the Yii-user module for your application but you want to write a module just like that, then just download the Yii-user module then study how it works. Not everyone here will always be available to answer your questions, but the code will always be there for you to experiment on. Besides, example is the best teacher so as they say.
To pass variables to the view set the 2nd parameter in the render method. See below. In your views, you can then use the key of the array as a variable. In this case, $model.
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id)
{
$model = User::model()->findByPk((int) $id);
if ($model === null)
throw new CHttpException(404, 'The requested page does not exist.');
return $model;
}
/**
* Displays a particular model.
* @param integer $id the ID of the model to be displayed
*/
public function actionView($id)
{
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
The view.php file is used to display details about the record.
If you look at the default files after CRUDing your table, you’ll see index.php and _view.php. These two normally work in conjunction with eachother. Ex - index.php calls _view.php. The _view.php is simply a template file and the index.php passes the dataProvider variable to that view to display.
To answer you first question, the actionView() method should use the view.php file and the actionIndex() method should use the index.php which will call the _view.php.
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity {
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
private $_id = NULL;
public function authenticate() {
$criteria = new CDbCriteria;
$criteria->condition = 'username = :username';
$criteria->params = array(':username' => $this->username);
$user = User::model()->find($criteria);
if (!isset($this->username) || NUll === $user || !isset($this->password)) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} else if ($user->encryptPassword($this->password) !== $user->password) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} else {
$this->errorCode = self::ERROR_NONE;
$this->_id = $user->id;
if (NULL === $user->last_login_time) {
$lastLoginTime = time();
} else {
$lastLoginTime = strtotime($user->last_login_time);
}
$this->setState('lastLoginTime', $lastLoginTime);
}
return!$this->errorCode;
}
public function getId() {
return $this->_id;
}
}
Its not possible to use Yii::app()->user in afterLogin() method when allowAutoLogin is true. I mean for user who doesnt use "remember me" its ok, but it fails when afterLogin() is called inside restoreFromCookie method.