调用成员函数helper()时为null

问题描述 投票:0回答:1

我正在尝试找出codeigniter,目前无法将其插入数据库。我一直在关注从数据库读取成功的官方文档。我的完整错误:

遇到未捕获的异常类型:错误

消息:调用成员函数helper()为空

文件名:/Library/WebServer/Documents/codeChallenge/system/libraries/Form_validation.php

行号:147

回溯:

文件:/Library/WebServer/Documents/codeChallenge/application/models/People_model.php行:6函数:__ construct

文件:/Library/WebServer/Documents/codeChallenge/application/controllers/People.php行:7功能:型号

文件:/Library/WebServer/Documents/codeChallenge/index.php线:315功能:require_once

我已经尝试寻找解决方案达数小时之久,没有成功。

控制器:

class People extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('people_model');
        $this->load->helper('url_helper');
    }

    public function index()
    {
        $data['people'] = $this->people_model->get_people();
        $data['title'] = 'People';

        $this->load->view('templates/header', $data);
        $this->load->view('people/index', $data);
        $this->load->view('templates/footer');
    }

    public function view($id = NULL)
    {
        $data['people_item'] = $this->people_model->get_people($id);

        if (empty($data['people_item']))
        {
            show_404();
        }

        $this->load->view('templates/header', $data);
        $this->load->view('people/view', $data);
        $this->load->view('templates/footer');  
    }


    public function create()
    {
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('age', 'Age', 'required');
        $this->form_validation->set_rules('city', 'City', 'required');
        $this->form_validation->set_rules('province', 'Province', 'required');
        $this->form_validation->set_rules('country', 'Country', 'required');

        if ($this->form_validation->run() === FALSE)
        {
            $this->load->view('templates/header', $data);
            $this->load->view('people/create');
            $this->load->view('templates/footer');
        }
        else
        {
            $this->people_model->set_people();
            $this->load->view('people/success');
        }
    }
    } 

型号:

    <?php
    class People_model extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function get_people($id = FALSE)
    {
        if ($id === FALSE)
        {
            $query = $this->db->get('people');
            return $query->result_array();
        }

        $query = $this->db->get_where('people', array('id' => $id));
        return $query->row_array();
    }

    public function set_people()
    {
        $this->load->helper('url');

        $data = array (
            'name' => $this->input->post('name'),
            'age' => $this->input->post('age'),
            'city' => $this->input->post('city'),
            'province' => $this->input->post('province'),
            'country' => $this->input->post('country')
        );

        return $this->db->insert('people', $data);
    }
    }

查看:

    <h2>Create a record</h2>

    <?php echo validation_errors(); ?>

    <?php echo form_open('people/create'); ?>

    <label for="name">Name</label>
    <input type="text" name="name" /><br />

    <label for="age">Age</label>
    <input type="text" name="age" /><br />

    <label for="city">City</label>
    <input type="text" name="city" /><br />

    <label for="province">Province</label>
    <input type="text" name="province" /><br />

    <label for="country">Country</label>
    <input type="text" name="country" /><br />

    <input type="submit" name="submit" value="Create people item" />

    </form>

路线:

    $route['people/create'] = 'people/create';
    $route['people/(:any)'] = 'people/view/$1';
    $route['people'] = 'people';
    $route['(:any)'] = 'pages/view/$1';
    $route['default_controller'] = 'pages/view';

任何输入将不胜感激。

php database api codeigniter codeigniter-3
1个回答
0
投票

在您的示例中,People_model()是模型,但是您将其扩展为控制器。

变化:

class People_model extends CI_Controller 

进入

class People_model extends CI_Model 
© www.soinside.com 2019 - 2024. All rights reserved.