raju
raju

Reputation: 219

Managing views in CodeIgniter

I want to create different views for admin and user. I have created a page called members which will be displayed after user logs in successfully. But how to create another view for admin.
This is my controller:

<?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');
  }
  }
  }

after login_validation is successful the user will be moved to member page.in same same way if admin logs in he should be moved to admin page.

this is my model.

<?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";
         }
         }

         }

i am very new to mvc and codeigniter

Upvotes: 0

Views: 77

Answers (2)

Deniz B.
Deniz B.

Reputation: 2562

I think you should create seperate controller functions for this task. Please see the example:

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');
   }
  }
}

And you should create a file called MY_Controller.php under application/core folder to validate logged users.

    <?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');
      }
    }
  }

In this code, I made a login validation with sessions. If you see MY_Controller file, you should define a session variable called "logged" with true value. You can modify it by your needs.

If you use this structure, you can easily seperate admin and user views. Please don't forget to add session keys called "type" and "logged" (or something else).

If you have any questions please don't hesitate to ask.

Upvotes: 1

Diego Arboleda
Diego Arboleda

Reputation: 82

I don't know if this is what you want, but just for create another view, simply create other method for login and then load the new view.

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'); 

    }
}

then just point your browser to project/adminlogin, or you can create a new controller/view for the admin.

Upvotes: 1

Related Questions