在CodeIgniter中管理视图

时间:2016-11-12 11:29:33

标签: php codeigniter

我想为管理员和用户创建不同的视图。我创建了一个名为members的页面,该页面将在用户成功登录后显示。但是如何为管理员创建另一个视图 这是我的控制者:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Project extends CI_Controller {


public function index()
{
    $this->login();
}

public function members()
{


    $this->load->view('members');

}
 public function singup()
{
    $this->load->view('singup');
}
public function login()
{
    $this->load->view('login');
}
public function login_validation()
{
 $this->load->library('form_validation');
     $this->form_validation- >set_rules('email','Email','required|trim|callback_validate_credentials');
     $this->form_validation->set_rules('password','Password','required|md5');
    if($this->form_validation->run())
{
$this->load->view('members');

}else{
$this->load->view('login'); 
 }
 }

 function validate_credentials()
 {
 $this->load->model('Project_users');
 if($this->Project_users->can_login_in())
 {
 return true;
 }
 else
 {
  $this->form_validation->set_message('validate_credentials','incorrect username/password');
 return false;
 }
 }
 public function singup_validation()
 {
 $this->load->library('form_validation');
     $this->form_validation->set_rules('email','Email','required|trim|valid_email|is_unique[users.email]');
     $this->form_validation->set_rules('password','Password','required');
     $this->form_validation->set_rules('cpassword','Confrim Password','required|matches[password]');
      $this->form_validation->set_message('is_unique',"this email is already registed");
     if($this->form_validation->run())
  {
  $this->load->model('project_users');      
  $this->project_users->add_temp_user();
  }else{
  echo "please singup";
  $this->load->view('singup');
  }
  }
  }

在login_validation成功后,用户将被移动到成员页面。如果管理员登录他应该被移动到管理页面。

这是我的模特。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Project_users extends CI_Model {
function can_login_in()
{
     $this->db->where('email',$this->input->post('email'));
     $this->db->where('password',md5($this->input->post('password')));

    $query = $this->db->get('users');
    if($query->num_rows() == 1){

        return true;
    }else{
        return false;
    }
    }


    public function add_temp_user()
    {
     $data=array('email'=>$this->input- >post('email'),'password'=>md5($this->input->post('password')));
      $query=$this->db->insert('users',$data);
   if($query){
    echo "inserted";
     }
     else{
         echo "colud not add to db";
         }
         }

         }

我是mvc和codeigniter的新手

2 个答案:

答案 0 :(得分:1)

我不知道这是否是你想要的,但只是为了创建另一个视图,只需创建其他登录方法然后加载新视图。

public function adminlogin()
{
    $this->load->view('adminlogin');
}

public function admingloginvalidation()
{

    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','required|trim|callback_validate_credentials');
    $this->form_validation->set_rules('password','Password','required|md5');
    if ($this->form_validation->run()) {

        $this->load->view('adminmembers');

    } else {

        $this->load->view('adminlogin'); 

    }
}

然后只需将浏览器指向project / adminlogin,或者您可以为管理员创建新的控制器/视图。

答案 1 :(得分:1)

我认为您应该为此任务创建单独的控制器功能。请参阅示例:

Auth.php // controller

class Auth extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model('Auth_model');
  }

  public function login() {
    $this->load->view('login'); //name of your view
  }

  public function check() {
    $this->form_validation->set_rules('username', 'username', 'required');
    $this->form_validation->set_rules('password', 'password', 'required');

    if ($this->form_validation->run() == false) {
      $this->login();
    }
    else {
      $this->Auth_model->check();
    }
  }
}

Auth_model.php // model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth_model extends CI_Model {

  public function check() {
    // put your login check codes here and if it success, redirect to home (e.g. 'redirect("home")') and don't forget to define a session which includes type and logged keys. 
  }
}

Home.php // controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Logged_Controller {

  public function home() {
   if ($this->session->userdata('type') == 'admin') {
    $this->load->view('admin');
   }
   elseif ($this->session->userdata('type') == 'user') {
    $this->load->view('user');
   }
   else {
    redirect('login');
   }
  }
}

您应该在MY_Controller.php文件夹下创建一个名为application/core的文件,以验证已记录的用户。

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Logged_Controller extends CI_Controller {
      public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged')) {
          $this->session->set_flashdata('danger', 'Please login!');
          redirect('login');
      }
    }
  }

在此代码中,我使用会话进行了登录验证。如果你看到MY_Controller文件,你应该定义一个名为“logged”的会话变量,其值为true。您可以根据需要进行修改。

如果使用此结构,则可以轻松分离管理员和用户视图。请不要忘记添加名为“type”和“logged”(或其他)的会话密钥。

如果您有任何疑问,请随时提出。