Ion Auth配置文件页面

时间:2015-05-20 21:27:58

标签: codeigniter ion-auth

我想了解如何为ion auth codeigniter创建个人资料页面的分步教程。

当登录用户单击链接用户配置文件链接时,它会打开用户配置文件页面并在表单中检索用户的所有详细信息,以便用户可以更新。我希望这是管理员用户。

谢谢:)

1 个答案:

答案 0 :(得分:2)

我最终这样做了:

控制器下面的

<?php

已定义('BASEPATH')或退出('不允许直接访问脚本');

类用户扩展了MY_Controller {

function __construct() {
    parent::__construct();
    $this->load->library('ion_auth');
}

public function index() {

}

public function login() {

    if ($this->input->post()) {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('identity', 'Identity', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('remember', 'Remember me', 'integer');
        if ($this->form_validation->run() === TRUE) {
            $remember = (bool) $this->input->post('remember');
            if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember)) {
                redirect('dashboard', 'refresh');
            } else {
                $this->session->set_flashdata('message', $this->ion_auth->errors());
                redirect('admin/user/login', 'refresh');
            }
        }
    }
    $data['main_content'] = 'admin/login';
    $this->load->view('includes/template', $data);
}

public function logout() {
    $this->ion_auth->logout();
    redirect('admin/user/login', 'refresh');
}

public function profile() {
    $user = $this->ion_auth->user()->row();
    //print_r($user);
    $this->data['user'] = $user;
    //var_dump($user);
    $this->load->library('form_validation');
    $this->form_validation->set_rules('first_name', 'First name', 'trim');
    $this->form_validation->set_rules('last_name', 'Last name', 'trim');
    $this->form_validation->set_rules('company', 'Company', 'trim');
    $this->form_validation->set_rules('phone', 'Phone', 'trim');

    if ($this->form_validation->run() === FALSE) {

        $this->load->view('admin/edit_profile', $this->data);

    } else {
        $data = array(
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'company' => $this->input->post('company'),
            'phone' => $this->input->post('phone')
        );
        if (strlen($this->input->post('password')) >= 6)
            $new_data['password'] = $this->input->post('password');
        $this->ion_auth->update($user->id, $data);


        redirect('dashboard', 'refresh');
    }

    //$this->load->view('admin/edit_profile', $data);

}

}

然后是观点:

<h1><?php echo lang('edit_user_heading');?></h1>

  <p>
        <?php echo lang('edit_user_fname_label', 'first_name');?> <br />
        <?php echo form_input($first_name);?>
  </p>

  <p>
        <?php echo lang('edit_user_lname_label', 'last_name');?> <br />
        <?php echo form_input($last_name);?>
  </p>

  <p>
        <?php echo lang('edit_user_company_label', 'company');?> <br />
        <?php echo form_input($company);?>
  </p>

  <p>
        <?php echo lang('edit_user_phone_label', 'phone');?> <br />
        <?php echo form_input($phone);?>
  </p>

  <p>
        <?php echo lang('edit_user_password_label', 'password');?> <br />
        <?php echo form_input($password);?>
  </p>

  <p>
        <?php echo lang('edit_user_password_confirm_label', 'password_confirm');?><br />
        <?php echo form_input($password_confirm);?>
  </p>

  <?php if ($this->ion_auth->is_admin()): ?>

      <h3><?php echo lang('edit_user_groups_heading');?></h3>
      <?php foreach ($groups as $group):?>
          <label class="checkbox">
          <?php
              $gID=$group['id'];
              $checked = null;
              $item = null;
              foreach($currentGroups as $grp) {
                  if ($gID == $grp->id) {
                      $checked= ' checked="checked"';
                  break;
                  }
              }
          ?>
          <input type="checkbox" name="groups[]" value="<?php echo $group['id'];?>"<?php echo $checked;?>>
          <?php echo htmlspecialchars($group['name'],ENT_QUOTES,'UTF-8');?>
          </label>
      <?php endforeach?>

  <?php endif ?>

  <?php echo form_hidden('id', $user->id);?>
  <?php echo form_hidden($csrf); ?>

  <p><?php echo form_submit('submit', lang('edit_user_submit_btn'));?></p>