我无法从codeigniter中的另一个控制器调用静态函数。为什么?

时间:2014-03-05 09:55:54

标签: codeigniter

我已经开始在codeigniter

中创建一个名为用户的控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {

        public function __construct() {
            parent::__construct();
            $this->load->helper(array('form', 'url'));
            $this->load->library(array( 'encrypt', 'form_validation' ));
        }

        public function index()
        {                
        }

        /*
         * Create loginform
         * Parameters:  void
         * Return:      html of loginform;
         */
        public static function loginform() {

                // Login form setup
                $loginform_data = array();                  
                $loginform_data['attributes']       = array('id'  => 'loginform');  
                $loginform_data['username']         = array(
                                                            'name'        => 'username',
                                                            'id'          => 'username',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );                
                $loginform_data['pass']             = array(
                                                            'name'        => 'pass',
                                                            'id'          => 'pass',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );      

                $contentdata = array();
                $contentdata['loginform'] = $this->load->view('partials/forms/login', $loginform_data, true);
                return $contentdata;
        }

        /*
         * Check login username, password from form
         * Parameters:  void
         * Return:      void;
         */
        public function login() {
                $name = $this->input->post('username');
                $pass = $this->input->post('pass');

                $this->form_validation->set_rules('username', 'Användarnamn', 'required');
                $this->form_validation->set_rules('pass', 'Lösenord', 'required');

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

我可以通过url调用user / login - 函数。但我不能从另一个控制器调用User::loginform()我不应该这样做吗?

以下是我正在尝试的内容:(来自我的家庭班级)

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    public function index()
    {
                //Create login and register-forms
                $this->load->helper('form');

                //Registration form setup               
                $registerform_data = array();                  
                $registerform_data['attributes']    = array('id'  => 'registerform');  
                $registerform_data['company']       = array(
                                                            'name'        => 'name-company',
                                                            'id'          => 'name-company',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );                
                $registerform_data['orgnr']         = array(
                                                            'name'        => 'orgnr-company',
                                                            'id'          => 'orgnr-company',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );        
                $registerform_data['contact']       = array(
                                                            'name'        => 'contact-company',
                                                            'id'          => 'contact-company',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );                      
                $registerform_data['phonecompany']  = array(
                                                            'name'        => 'phone-company',
                                                            'id'          => 'phone-company',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );                          
                $registerform_data['emailcompany']  = array(
                                                            'name'        => 'email-company',
                                                            'id'          => 'email-company',
                                                            'value'       => '',
                                                            'maxlength'   => '100'                                                    
                                                    );   




                //What content to pass to view       
                $contentdata = array();
                $contentdata['loginform'] = User::loginform();
                $contentdata['registerform'] = $this->load->view('partials/forms/registration', $registerform_data, true);                

                $this->load->view('home', $contentdata);
    }
}

$contentdata['loginform'] = User::loginform();给了我错误:Fatal error: Class 'User' not found in C:\Program...

我缺少什么?

1 个答案:

答案 0 :(得分:1)

在扩展Home课程时,请延伸到User控制器,如

require_once(APPPATH.'controllers/user.php');
class Home extends User {

因为你需要从User类中提取函数,然后它将Inherit父类User起作用而且login_form是你的 public User控制器中使用strong>功能,您现在可以在Home控制器中调用此功能。此处不需要使用Static我认为是这样。

还有一种方法可以做到这一点。只需在login_form中编写helper函数并在两个控制器上调用它,然后您的问题就可以解决了。

修改 @IJas 说,我们需要包含您要扩展的控制器文件。