Codeigniter 3 - 遇到 PHP 错误,严重性:通知,消息:未定义 属性:Home::$home

Codeigniter 3 - A PHP Error was encountered, Severity: Notice, Message: Undefined property: Home::$home

我即将推出一个我使用 codeigniter3 构建的网站。我已经进行了配置更改,例如 base_url、数据库等。但它向我显示了这样的错误:


Message: Undefined property: Home::$home

Filename: controllers/Home.php

Line Number: 18

Backtrace:

File: /home/u8460348/public_html/application/controllers/Home.php
Line: 18 Function: _error_handler

File: /home/u8460348/public_html/index.php Line: 294 Function:
require_once ```


Message: Call to a member function select() on null

Filename: /home/u8460348/public_html/application/controllers/Home.php

Line Number: 18

Backtrace:

File: /home/u8460348/public_html/index.php Line: 294 Function:
require_once ```

所以,这是我的控制器:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Home extends My_Controller
{

    public function index($page = null)
    {
        $data['title']  = 'Home | Omid Health Style';
        $data['content']    = $this->home->select(
            [
                'product.id', 'product.slug', 'product.title AS product_title', 'product.description',
                'product.image', 'product.price', 'product.is_available', 'product.weight',
                'category.title AS category_title', 'category.slug AS category_slug'
            ]
        )
            ->join('category')
            ->where('product.is_available', 1)
            ->paginate($page)
            ->get();
        $data['total_rows'] = $this->home->where('product.is_available', 1)->count();

        $this->home->table = 'blog';
        $data['blogs'] = $this->home->select(
            [
                'blog.id', 'blog.slug', 'blog.title AS blog_title', 'blog.description', 'blog.content',
                'blog.image', 'blog_category.title AS blog_category_title', 'blog_category.slug AS blog_category_slug'
            ]
        )
            ->join('blog_category')
            ->paginate($page)
            ->get();
        $data['total_rows'] = $this->home->count();

        $data['page']   = 'pages/home/index';

        $this->view($data);
    }
}

这是我的 Home_model.php :

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class Home_model extends MY_Model
{

    public $table    = 'product';
    protected $perPage  = 8;
}

/* End of file Home_model.php */

这是我的核心文件夹,因为我做了这样的自定义配置:

<?php

defined('BASEPATH') or exit('No direct script access allowed');

class My_Controller extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $model = strtolower(get_class($this));

        if (file_exists(APPPATH . 'models/' . $model . '_model.php')) {
            $this->load->model($model . '_model', $model, true);
        }
    }


    // Load view with Default Layouts
    public function view($data)
    {
        $this->load->view('layouts/app', $data);
    }
}

如果我是 运行 本地主机中的程序,它完全可以正常工作。为什么我在生产服务器上会收到此错误?

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

将此添加到您的家庭控制器

从您收到的错误消息来看,重要的部分是

Message: Call to a member function select() on null

这意味着您的模型未加载!

由于您的代码可以在您的本地主机环境中运行,但不能在生产服务器上运行,因此错误很可能是由于应用错误 Naming Conventions:

  1. File names must be capitalized. For example: Myclass.php Class
  2. declarations must be capitalized. For example: class Myclass Class
  3. names and file names must match.