我无法在 Codeigniter 上创建 class 的实例

I can't create an instance of a class on Codeigniter

我正在尝试创建 class 的实例,但出现以下错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: DM_TemplateLoader::$load

Filename: libraries/Form_validation.php

Line Number: 147

Backtrace:

File: C:\xampp\htdocs\DataMigrator\ci_my_app\controllers\DM_FormValidator.php Line: 13 Function: __construct

File: C:\xampp\htdocs\DataMigrator\index.php Line: 292 Function: require_once

Fatal error: Call to a member function helper() on a non-object in C:\xampp\htdocs\DataMigrator\ci_my_sys\libraries\Form_validation.php on line 147 A PHP Error was encountered

Severity: Error

Message: Call to a member function helper() on a non-object

Filename: libraries/Form_validation.php

Line Number: 147

Backtrace:

这是我的代码:

DM_FormValidator.php

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

class DM_FormValidator extends CI_Controller {

    public function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('sender_host', 'Host (Sender)', 'required');

        if ($this->form_validation->run() == FALSE) {           
            require_once APPPATH . 'controllers/DM_TemplateLoader.php';
            $template_loader = new DM_TemplateLoader();
        }
    }
}

DM_TemplateLoader.php

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

class DM_TemplateLoader extends CI_Controller {

    public function view($page = 'home') {  

        // Loading the URL helper
        $this->load->helper('url');     

        if (file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
            $data['page_title'] = 'Data Migrator: Home';

            // Building the template
            $this->load->view('template/header', $data);
            $this->load->view('pages/' . $page);
            $this->load->view('template/footer');
        }
        else {
             show_404();
        }   
    }
}

我做错了什么?

您正试图在另一个控制器中加载一个控制器。你不应该那样做。如我所见,您想调用自己的函数而不是直接加载视图。您有多种解决方案可以做到这一点,这里是一个:

创建库

在您的 application/libraries 文件夹中创建 Layout.php(或任何您想要的)

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

class Layout
{
    protected $ci;

    public function __construct()
    {
        $this->ci =& get_instance();
    }

    public function load_template($view)
    {
        // Loading the URL helper
        $this->ci->load->helper('url');     

        if (file_exists(APPPATH . 'views/pages/' . $page . '.php')) 
        {
            $data['page_title'] = 'Data Migrator: Home';

            // Building the template
            $this->ci->load->view('template/header', $data);
            $this->ci->load->view('pages/' . $page);
            $this->ci->load->view('template/footer');
        }
        else {
            show_404();
        }   
    }

}

加载你的图书馆

之后,在你的 application/config/autoload.php

$autoload['libraries'] = array('layout', yourotherstuff);

使用它

最后,在您的控制器中 DM_FormValidator 而不是实例化您的 class 只需调用库 :

$view = "mypage";
$this->layout->load_template($data);

您也可以用您的重载基础 load->view() (system/core/Loader.php)。查看文档:http://www.codeigniter.com/user_guide/general/core_classes.html