Codeigniter:从另一个控制器获取变量

Codeigniter : get variable from another controller

我有以下控制器来显示登录表单(第一页)

public function index()
{
    $data['form_type']='dologin';
    $data['username_label']='Username';
    $data['password_label']='Password';
    $data['username_name']='usernamenya';//name of the textbox username
    $data['password_name']='passwordnya';//name of the textbox password
    $data['username_value']='';
    $data['password_value']='';
    $data['fieldset_text']='Silahkan Login';        
    $data['fieldset_close_text'] = '</div></div>';
    $data['form_close_text']='</div></div>';
    $data['clear_name']='Hapus';
    $data['submit_name']='Login';

    $this->load->helper('form');
    $this->load->view('header');
    $this->load->view('content_login',$data);
    $this->load->view('footer');        
}

单击按钮后,它将重定向到 dologin 控制器,我已经在 dologin 控制器

上创建了 index 函数
class dologin extends CI_Controller {

public function index()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('','','');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->load->view('formsuccess');
    }
}   
      }

在需要控件名称的form_validation_rules中,我想从另一个控制器获取它,因此,如果我们在index控制器中更改名称,dologin控制器将获胜'打破并继续这个过程

我该怎么做? 还是有其他方法可以实现我的想法?

我不确定我是否完全理解您的问题,但我会按照以下方式处理此类问题:


假设我们有一个基本的 html 登录表单,我们在其中要求提供电子邮件和密码:

<form method="post" action="<?php echo site_url("myController/login"); ?>">
    <input type="text" name="login_mail" required>
    <input type="password" name="login_pass" required>
    <button type="submit">Login</button>                
</form>

如您所见,提交表单将调用控制器 myController 中的函数 login(),如 action 属性中所定义。

让我们看看 login() 函数 :

class MyController extends CI_Controller
{
    public function login()
    {
        $this->load->library('form_validation');

        //Here we set the validation rules for our two fields. We must use the same names as defined in out html for the `name` attribute.
        $this->form_validation->set_rules('login_mail', 'Email', 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('login_pass', 'Password', 'trim|required|xss_clean');

        if ($this->form_validation->run()) //Success
        {
            //Get the submited values
            $email = $this->input->post("login_mail");
            $password = $this->input->post("login_pass");

            //Your stuff here

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