CodeIgniter - 无法运行$ this-> input-> post('inputName'); 在模块化扩展中

As my title, I tried call that method but I got an error:

Fatal error: Call to a member function post() on a non-object in C:\xampp\htdocs\cifirst\application\modules\front\controllers\shopping.php on line 11

If I create a controller not in module, that method I can use very easy but in this case can not (everything code in method below can not run). This is my code:

 public function add_to_cart() {
  $data = array(
   'id' => $this->input->post('productId'), // line 11
   'name' => $this->input->post('productName'),
   'price' => $this->input->post('productPrice'),
   'qty' => 1,
   'options' => array('img' => $this->input->post('productImg'))
  );

  $this->load->library('MY_Cart');
  $this->cart->insert($data);

  //redirect($_SERVER['HTTP_REFERER']);
  //echo $_POST['productId'].'-'.$_POST['productName'];
 }

And this code doesn't work too:

public function __construct() {
    $this->load->library('cart');
    $this->load->helper('form');
}

I'm using XAMPP 1.8.1, CodeIgniter 2.1.3 and newest MX. Please help me!

When you're using CodeIgniter functions outside of controllers, models, and views you need to get an instance of Codeigniter first.

class MyClass {
    private $CI;

    function __construct() {
        $this->CI =& get_instance();
        $this->CI->load->library('cart');
        $this->CI->load->helper('form');
    }

    public function someFunction() {
        $var = $this->CI->input->post("someField");
    }
}

If you are calling:

$this->input->post('productId');

inside controller than the problem is with your constructor declaration or your class name Your construct part should contain code like this:

Class Home extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->CI->load->library('cart');
        $this->CI->load->helper('form');
    }


     public function add_to_cart() 
     {
          $data = array(
                  'id' => $this->input->post('productId'), // line 11
                  'name' => $this->input->post('productName'),
                  'price' => $this->input->post('productPrice'),
                  'qty' => 1,
                  'options' => array('img' => $this->input->post('productImg'))
                  );


      }
}

It will work fine if you are calling from helpers function or any other classes than you should do something like this:

  function __construct()
  {
        parent::__construct();
        $this->CI->load->library('cart');
        $this->CI->load->helper('form');

        $this->CI =& get_instance();
  } 


  public function add_to_cart() 
  {
     $data = array(
        'id' => $this->CI->input->post('productId'), // line 11
        'name' => $this->CI->input->post('productName'),
        'price' => $this->CI->input->post('productPrice'),
        'qty' => 1,
        'options' => array('img' => $this->CI->input->post('productImg'))
        );
 }