【问题标题】:Codeiginiter Code working well in local environtment but not working when deploying at domainCodeigniter 代码在本地环境中运行良好,但在域部署时无法运行
【发布时间】:2021-11-28 23:18:29
【问题描述】:

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

遇到 PHP 错误 严重性:通知

消息:未定义的属性:Home::$home

文件名:controllers/Home.php

行号:11

回溯:

文件:/home/u1532211/public_html/application/controllers/Home.php 行:11 函数:_error_handler

文件:/home/u1532211/public_html/index.php 行:321 功能: 需要一次

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

消息:在 null 上调用成员函数 select()

文件名:/home/u1532211/public_html/application/controllers/Home.php

行号:11

回溯:

文件:/home/u1532211/public_html/index.php 行:321 功能: 需要一次

这是我的控制器

<?php

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

class Home extends MY_Controller 
{

    public function index($page = null)
    {
        $data['title']  ='Homepage';
        $data['content']    = $this->home->select(
                [   
                    'product.id', 'product.title AS product_title', 
                    'product.description', 'product.image', 
                    'product.price', 'product.is_available',
                    '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();
        $data['pagination'] = $this->home->makePagination(
            base_url('home'), 2, $data['total_rows']
        );
        $data['page']   ='pages/home/index';

        $this->view($data);
    }

}

这是我的模型

<?php

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

class Home_model extends MY_Model 
{
    protected $table    = 'product';
    protected $perPage  = 4;
    

}

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

<?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);
    }

}

模型核心

<?php

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

class MY_Model extends CI_Model 
{
    protected $table ='';
    protected $perPage =5;

    public function __construct()
    {
        parent::__construct();
        
        if (!$this->table) {
            $this->table = strtolower(
                str_replace('_model', '', get_class($this))
            );
        }
    }


    /**
     * Fungsi Validasi Input
     * Rules : Dideklarasikan dalam masing masing model
     */
    public function validate()
    {
        $this->load->library('form_validation');

        $this->form_validation->set_error_delimiters(
            '<small class="form-text text-danger">', '</small>'
        );
        $validationRules =$this->getValidationRules();
        
        $this->form_validation->set_rules($validationRules);

        return $this->form_validation->run();
    }

    /**
     * Seleksi data per-kolom
     * Chain Method
     */

    public function select($columns)
    {
        $this->db->select($columns);
        return $this;
    }

    /**
     * Mencari suatu data pada kolom tertentu dengan data yang sama
     * Chain Method
     */
    public function where($column, $condition)
    {
        $this->db->where($column, $condition);
        return $this;
    }

    /**
     * Mencari suatu data pada kolom tertentu dengan data yang mirip
     */
    public function like($column, $condition)
    {
        $this->db->like($column, $condition);
        return $this;
    }

    /**
     * Mencari suatu data selanjutnya pada kolom tertentu dengan data yang mirip
     */
    public function orLike($column, $condition)
    {
        $this->db->or_like($column, $condition);
        return $this;
    }


    /**
     * Menggabungkan table yang berelasi yang memiliki foreign key id_namatable
     */
    public function join($table, $type = 'left')
    {
        $this->db->join($table, "$this->table.id_$table= $table.id", $type);
        return $this;
    }

    /**
     * Mengurutkan data dari hasil query dan kondisi
     * Chain Method
     */
    public function orderBy($column, $order = 'asc')
    {
        $this->db->order_by($column, $order);
        return $this;
    }

    /**
     * Menampilkan suatu data dari hasil query dan kondisi
     * Hasil Akhir Chain Method
     */
    public function first()
    {
        return $this->db->get($this->table)->row();
    }

    /**
     * Menampilkan banyak data dari hasil query dan kondisi
     * Hasil Akhir Chain Method
     */
    public function get()
    {
        return $this->db->get($this->table)->result();
    }

    /**
     * Menampilkan nilai jumlah data dari hasil query dan kondisi
     */
    public function count()
    {
        return $this->db->count_all_results($this->table);
    }

    /**
     * Menyimpan data baru ke dalam suatu table
     */
    public function create($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * Mengubah data yang ada pada suatu table dengan data baru
     */
    public function update($data)
    {
        return $this->db->update($this->table, $data);
    }

    /**
     * Menghapus suatu data dari hasil query dan kondisi
     */
    public function delete()
    {
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }

    /**
     * Menentukan limit data untuk ditampilkan
     */
    public function paginate($page)
    {
        $this->db->limit(
            $this->perPage,
            $this->calculateRealOffset($page)
        );
        return $this;
    }

    /**
    * Menggantikan offset dengan nilai sesuai halaman 
    */    
    public function calculateRealOffset($page)
    {
        if (is_null($page)|| empty($page)) {
            $offset = 0;
        } else {
            $offset = ($page * $this->perPage) -$this->perPage;
        }

        return $offset;
    }
    /**
     * Membuat pagination dengan style bootstrap 4
     */
    public function makePagination($baseUrl, $uriSegment, $totalRows=null)
    {
        $this->load->library('pagination');

        $config = [
            'base_url'          => $baseUrl,
            'uri_segment'       => $uriSegment,
            'per_page'          => $this->perPage,
            'total_rows'        => $totalRows,
            'use_page_numbers'  => true,
            'full_tag_open'     => '<ul class="pagination">',
            'full_tag_close'    => '</ul>',
            'attributes'        => ['class' => 'page-link'],
            'first_link'        => false,
            'last_link'         => false,
            'first_tag_open'    => '<li class="page-item">',
            'first_tag_close'   => '</li>',
            'prev_link'         => '&laquo',
            'prev_tag_open'     => '<li class="page-item">',
            'prev_tag_close'    => '</li>',
            'next_link'         => '&raquo',
            'next_tag_open'     => '<li class="page-item">',
            'next_tag_close'    => '</li>',
            'last_tag_open'     => '<li class="page-item">',
            'last_tag_close'    => '</li>',
            'cur_tag_open'      => '<li class="page-item active"><a href="#" class="page-link">',
            'cur_tag_close'     => '<span class="sr-only">(current)</span></a></li>',
            'num_tag_open'      => '<li class="page-item">',
            'num_tag_close'     => '</li>',
        ];

        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }
}

我已经在我的本地主机上测试过了,它完全没问题,而且运行良好,但是为什么我在域中部署时会出现这个错误?

【问题讨论】:

  • 提示:数据库部分应该在模型中,你的 controllers/Home.php 中的第 11 行是什么?
  • @Vickel 在我的 Home.php 上是:$data['content'] = $this->home->select

标签: php codeigniter web-deployment


【解决方案1】:

考虑到您将 home 称为数据库实例。

错误在于 $this->home->select();这表示您正在尝试在 home 的帮助下调用数据库函数,但 home 从未被声明过,并且它不是数据库的实例。

以下是解决问题的步骤。

  1. 检查您在 config/database.php 中是否有任何名称为 home 的已配置数据库。如果您没有任何名称为 home 的东西,那么您可能正在使用默认的 DB 数组。那么你应该使用 $this->db->select()
  2. 检查是否在 config/autoload.php 中加载了 database 库。如果没有,则加载它
  3. 如果您已为项目 config/database.php 配置了多个数据库,则应在使用前加载该数据库。

例如:

如果您的 database.php 有 $db['default'] = array();$db['home'] = array(); 并且您想使用 home 数据库,那么您应该像这样实例化它

 $home = $this->load->database('home', TRUE);

然后你就可以使用它了。

 $home->select();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2013-06-23
    相关资源
    最近更新 更多