Dan
Dan

Reputation: 425

Codeigniter reference MY_Controller in form helper

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?

Upvotes: 0

Views: 364

Answers (3)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

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

Upvotes: 0

fletch
fletch

Reputation: 1641

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

Upvotes: 2

sharif2008
sharif2008

Reputation: 2798

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

Upvotes: 0

Related Questions