php Codeigniter ion auth 试图将登录用户的用户名放入字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9088008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter ion auth trying to get the user name of a logged in user in to a string
提问by 1ftw1
Hi team i am new to CI and ion auth and i want to find the best way to get the user name on the logged user and make it a string. I have try this and im getting an error.
嗨,团队,我是 CI 和 ion auth 的新手,我想找到在登录用户上获取用户名并将其设为字符串的最佳方法。我已经尝试过这个,但出现错误。
Object of class CI_Email could not be converted to string
类 CI_Email 的对象无法转换为字符串
This is my Controller
这是我的控制器
class Dashboard extends CI_Controller {
function index() {
$user = $this->ion_auth->user();
$email = $user->email;
$data['email']= $email;
$data['mainContent'] = 'dashboard_view';
$this->load->view('template', $data);
}
and view
并查看
<div id='dashboarWap'>
<?php echo form_open('dashboard/dashInput'); ?>
<?php echo $email; ?>
Any idea on why i am getting this error would be a big help thanks.
关于为什么我会收到此错误的任何想法都会有很大帮助,谢谢。
回答by Seabass
$this->ion_auth->user()
is a DB object.
$this->ion_auth->user()
是一个数据库对象。
$this->ion_auth->user()->row();
returns the user object which you can query for first_name, number_of_cats_owned, or whatever.
$this->ion_auth->user()->row();
返回您可以查询 first_name、number_of_cats_owned 或其他内容的用户对象。
https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/controllers/auth.php
https://github.com/benedmunds/CodeIgniter-Ion-Auth/blob/2/controllers/auth.php
回答by Louis
Ion Auth sets the username, email, user id, and last login time in session variables when the user logs in (have a look at ln 683 ish in the ion_auth_model'slogin function.)
当用户登录时,Ion Auth 在会话变量中设置用户名、电子邮件、用户 ID 和上次登录时间(查看ion_auth_model 的登录函数中的 ln 683 ish 。)
The simplest way to get this info is by using CI's session classlike so:
获取此信息的最简单方法是使用CI 的会话类,如下所示:
$username = $this->session->userdata( 'username' );
Of course, I abuse helpers so to make this even simpler I'll normally use a variant of this helper function:
当然,我滥用助手,所以为了让这更简单,我通常会使用这个助手函数的一个变体:
function userdata( $key, $val = null ){
$ci = &get_instance();
if ( $val !== null ){
$ci->session->set_userdata( $key, $val );
} else {
return $ci->session->userdata( $key );
}
}
This gives us a handy (& global scoped) function to call on as needed, wherever needed:
这为我们提供了一个方便的(和全局范围的)函数,可以根据需要在任何需要的地方调用:
$username = userdata( 'username' );
回答by Regolith
Ion auth already passes any data on logged in user through
Ion auth 已经通过了登录用户的任何数据
$this->data['users'] = $this->ion_auth->users()->result();
You can access user name by doing this
您可以通过执行此操作访问用户名
foreach ($users as $value) {
echo $value->first_name. " " . $value->last_name;
}
回答by wahmal
For get array data:
获取数组数据:
$this->ion_auth->user()->row_array();
For get object data:
获取对象数据:
$this->ion_auth->user()->row();
回答by bungdito
as simple as:
就像这样简单:
$this->ion_auth->user()->row()->username;