致命错误:在第18行的C:\ xampp \ htdocs \ cms \ application \ controllers \ admin \ user.php中调用未定义的方法User_m :: get()

时间:2016-07-21 18:13:47

标签: php codeigniter

当我打开链接' http://127.0.0.1/cms/index.php/admin/user/edit/1' 这是我的user_m模型

<?php

class User_m extends CI_Model{

    protected $_table_name = 'users';
    protected $_order_by = 'name';
    public $rules = array(

        'email' => array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'trim|required|valid_email|xss_clean'
            ),

        'password' => array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'trim|required'
            )
        );
    public $rules_admin = array(

        'name' => array(
            'field' => 'name',
            'label' => 'Name',
            'rules' => 'trim|required|valid_email|xss_clean'
            ),
        'order' => array(
            'field' => 'order',
            'label' => 'Order',
            'rules' => 'trim|is_natural'
            ),
        'email' => array(
            'field' => 'email',
            'label' => 'Email',
            'rules' => 'trim|required|valid_email|callback__unique_email|xss_clean'
            ),

        'password' => array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'trim|matches[password_confirm]'
            ),

        'password_confirm' => array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'trim|matches[password]'
            )
        );

    public function __construct()
       {
            parent::__construct();

       }

       public function login()
       {


            $user = $this->db->get_where('users', array(
                'email' => $this->input->post('email'),
                'password' =>$this->hash($this->input->post('password'))

                ), TRUE);

            if (count($user)) {
                $data = array(
                    'name' => $user->name,
                    'email' => $user->email,
                    'id' => $user->id,
                    'loggedin' => TRUE,
                );
                $this->session->set_userdata($data);
            }
       }

       public function logout()
       {
            $this->session->sess_destroy();
       }

       public function loggedin()
       {
            return (bool) $this->session->userdata('loggedin');
       }

       public function hash($string)
       {
            return hash('sha512', $string . config_item('encryption_key'));
       }


}

这是我的用户控制器

<?php
class User extends Admin_Controller{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->data['users'] = $this->db->get_where('users');
        $this->data['subview'] = 'admin/user/index';
        $this->load->view('admin/_layout_main', $this->data);
    }

    public function edit($id=NULL)
    {

            $id == NULL || $this->data['user'] = $this->user_m->get($id);
            $this->data['subview'] = 'admin/user/edit';
            $this->load->view('admin/_layout_main', $this->data);
    }   

    public function delete($id)
    {

    }



    public function login()
    {
        $dashboard = 'admin/dashboard';
        $this->user_m->loggedin() == FALSE || redirect($dashboard);
        $rules = $this->user_m->rules;
        $this->form_validation->set_rules($rules);

        if ($this->form_validation->run()==true) {

            if ($this->user_m->login() == TRUE) {
                redirect($dashboard);
            }else{
                $this->session->set_flashdata('error', 'That email and password combination does not exit');
                redirect('admin/user/login', 'refresh');
            }
        }

        $this->data['subview'] = 'admin/user/login';
        $this->load->view('admin/_layout_modal', $this->data);
    }

    public function logout()
    {
        $this->user_m->logout();
        redirect('admin/user/login');
    }
}

这是我的编辑文件。

<div class="modal-header">
       <h3><?php echo empty($user->id) ? 'Add a new user' : 'Edit user' . $user->name; ?></h3>

      </div>
      <div class="modal-body">

      <?php echo '<pre>' . print_r($this->session->userdata, TRUE) . '</pre>'; ?>
      <?php echo validation_errors(); ?>
        <?php echo form_open(); ?>
        <table class="table">
            <tr>
                <td>Email</td>
                <td><?php echo form_input('email'); ?></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><?php echo form_password('password'); ?></td>
            </tr>
            <tr>
                <td></td>
                <td><?php echo form_submit('submit', 'Log in', 'class="btn btn-primary"'); ?></td>
            </tr>
        </table>
        <?php echo form_close(); ?>
      </div>

这是我的索引文件

    <section>
    <h2>Users</h2>
    <?php echo anchor('admin/user/edit', '<i class="icon-plus"></i> Add a user'); ?>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Email</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>
        </thead>
        <tbody>

            <?php if(count($users)): foreach ($users->result() as $user): ?>

                <tr>
                    <td><?php echo anchor('admin/user/edit/' . $user->id, $user->email); ?><td>
                    <td><?php echo btn_edit('admin/user/edit/'. $user->id); ?><td>
                    <td><?php echo btn_delete('admin/user/delete/' . $user->id); ?><td>
                </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="3">We could not find any user</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
</section>

1 个答案:

答案 0 :(得分:0)

在控制器上的父构造区域中尝试加载模型,使得该控制器中的所有函数都可以访问模型,否则可以在编辑函数中加载模型

我也可以看到模型中没有 get function

public function __construct()
{
    parent::__construct();
    $this->load->model('user_m'); // Filename should be User_m.php same as class
}

public function edit($id=NULL)
{
    $this->load->model('user_m'); // Filename should be User_m.php same as class
    $id == NULL || $this->data['user'] = $this->user_m->get($id);
    $this->data['subview'] = 'admin/user/edit';
    $this->load->view('admin/_layout_main', $this->data);
}   

此外,我看到您的IP地址已加载到您的网址中。如果您在config.php中使用CI3版本,则必须设置此值。

$config['base_url'] = '';

$config['base_url'] = 'http://localhost/your_project_name/';