2015-07-11 148 views
3

我想从一个模块中的一个控制器连接到另一个模块中的另一个控制器。我想从我的登录模块到我的仪表板模块,并使用仪表板模块内的功能。基本上从我的登录模块切换到我的仪表板模块。这是我的登录控制器和我的仪表板控制器。Codeigniter需要从一个模块连接到另一个模块

class Login extends MX_Controller { 

function index() 
{ 
     $this->load->view('includes/header'); 
     $this->load->view('login_form'); 
     $this->load->view('includes/footer');  
} 

function validate_credentials() 
{  
     $this->load->model('membership_model'); 
     $query = $this->membership_model->validate(); 

     if($query) // if the user's credentials validated... 
     { 
       $data = array(
         'username' => $this->input->post('username'), 
         'is_logged_in' => true 
       ); 
       $this->session->set_userdata($data); 
       redirect('login/site/members_area'); 
     } 
     else // incorrect username or password 
     { 
       $this->index(); 
     } 
} 

function signup() 
{ 
     //$data['main_content'] = 'signup_form'; 
     //$this->load->view('includes/template', $data); 
     $this->load->view('includes/header'); 
     $this->load->view('signup_form'); 
     $this->load->view('includes/footer'); 
} 

function create_member() 
{ 
     $this->load->library('form_validation'); 

     // field name, error message, validation rules 
     $this->form_validation->set_rules('first_name', 'Name', 'trim|required'); 
     $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); 
     $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|callback_check_if_email_exists'); 
     $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[15]|callback_check_if_username_exists'); 
     $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]'); 
     $this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]'); 


     if($this->form_validation->run() == FALSE) // didn't validate 
     { 
       $this->load->view('includes/header'); 
       $this->load->view('signup_form'); 
       $this->load->view('includes/footer'); 
     } 

     else 
     {   
       $this->load->model('membership_model'); 

       if($query = $this->membership_model->create_member()) 
       { 
         $data['account created'] = 'Your account has been created. <br /> <br /> You may now login'; 

         $this->load->view('includes/header'); 
         $this->load->view('signup_successful', $data); 
         $this->load->view('includes/footer'); 

       } 
       else 
       { 
         $this->load->view('includes/header'); 
         $this->load->view('signup_form'); 
         $this->load->view('includes/footer'); 
       } 
     } 

} 

     function check_if_username_exists($requested_username) 
{ 
     $this->load->model('membership_model'); 

     $username_available = $this->membership_model->check_if_username_exists($requested_username); 

       if ($username_available) 
       { 
           return TRUE; 
       }else{ 
           return FALSE; 
       } 
} 

function check_if_email_exists($requested_email) 
{ 
     $this->load->model('membership_model'); 

     $email_available= $this->membership_model->check_if_email_exists($requested_email); 

       if ($email_available) 
       { 
           return TRUE; 
       }else{ 
           return FALSE; 
       } 
} 



function logout() 
{ 
     $this->session->sess_destroy(); 
     $this->index(); 
} 

} 


my second login controller 



class Site extends MX_Controller { 

public function __construct() 
    { 
    parent::__construct(); 
     $this->is_logged_in(); 
       $this->lang->load('login/login/', 'english'); 
    } 

function members_area() 
{ 
     $this->load->view('logged_in_area'); 

         //$this->load->module('dashboard/dashboard'); 

         //$this->load->view('home'); 
} 


function is_logged_in() 
{ 
         $is_logged_in = $this->session->userdata('is_logged_in'); 
         if(!isset($is_logged_in) || $is_logged_in != true) 
         { 
           echo 'You don\'t have permission to access this page. <a href="../login">Login</a>'; 
           die();  
           //$this->load->view('login_form'); 
         }   
} 

} 

控制器在我的仪表板模块具有被称为归属我试图连接和使用功能。仪表板的主功能与另一个模块有连接,但我无法从登录到仪表板的连接工作。此外,我的登录工作方式我需要通过我的members_area函数连接到仪表板模块。所有帮助非常感谢。

+1

你使用的模块支持哪个库? HMVC? – insanebits

+0

你知道如何在CI中重定向吗? – MaHDyfo

+0

您可能需要设置路线。 – user4419336

回答

1

假设你正在使用this library

从控制器,你可以使用Modules::load$this->load-module

$moduleInstance = $this->load->module('yourmodule'); 
// or 
$moduleInstance = Modules::load('yourmodule'); 

// Now you can call any public module controller method 
$moduleInstance->somePublicMethod($arg1, $argn); 
// you can also use $this->yourmodule as a model instance 

希望这有助于

+1

你也可以使用'Modules :: run('modulename/controller/function');' – user4419336

相关问题