0

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?

2
  • form_open('MY_Controller/login');
    – Dale
    Commented Jun 13, 2013 at 12:27
  • what is your default_controller name ?? Commented Jun 13, 2013 at 12:34

3 Answers 3

2

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');
1
  • Very nice, I like this since using MY_Controller is not good restful uri design to say the least. This way, the login function is always in context, literally speaking.
    – Dan
    Commented Jun 13, 2013 at 13:17
0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.