douxie2007
2013-06-13 12:22
浏览 74
已采纳

Codeigniter引用表单助手中的MY_Controller

If I have a callback function within MY_Controller that processes a form :

class MY_Controller extends CI_Controller{

    function login(){
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');

        if ($this->form_validation->run() == true){
            //do something
        }
    }

}

How should I go about referencing it with a form helper in my view?

echo form_open('?/login');

The reasoning behind this is that we have a login form within a header template that doesn't have a corresponding controller, it's being loaded before each view. We are hiding the login form with JS if session data is set, so it seems most logical to place the callback within MY_Controller, I'm just unsure how to access it with the form helper class. Any suggestions?

图片转代码服务由CSDN问答提供 功能建议

如果我在MY_Controller中有一个处理表单的回调函数:

   class MY_Controller扩展CI_Controller {
 
函数login(){
 $ this-> form_validation-> set_rules('username','Username','required'); 
 $ this->  form_validation-> set_rules('password','Password','required'); 
 
 if if($ this-> form_validation-> run()== true){
 //做某事
  } 
} 
 
} 
   
 
 

我应该如何在视图中使用表单助手来引用它? < pre> echo form_open('?/ login');

这背后的原因是我们在标题模板中有一个登录表单 t有一个相应的控制器,它在每个视图之前被加载。 如果设置了会话数据,我们用JS隐藏登录表单,所以将回调放在MY_Controller中似乎最合乎逻辑,我只是​​不确定如何使用表单助手类访问它。 有什么建议?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dpvr49226 2013-06-13 12:34
    已采纳

    Since you are placing login() inside of MY_Controller and extending CI_Controller, every controller gains that method. Then, you could use $this->router->fetch_class(); to get the name of the current controller, and append it before /login.

    Ex.

    var $controller = $this->router->fetch_class();
    echo form_open($controller.'/login');
    
    打赏 评论
  • doupa2871 2013-06-13 12:34

    Add two library-

     $this->load->helper('form');
     $this->load->library('form_validation');
    

    in your controller constructor and add this

    echo form_open('MY_Controller/login');
    

    in view

    打赏 评论
  • douniao7308 2013-06-13 12:35

    You can access login() function of MY_Controller like

    form_open('default_controller_name/login');
    

    if there is no login() function in your default_controller

    打赏 评论

相关推荐 更多相似问题