wildGhifari
wildGhifari

Reputation: 73

Codeigniter 3 - A PHP Error was encountered & An uncaught Exception was encountered

Hi erevyone i have some trouble when i'm about to upload my program to hosting. I've made configuration changes such as base_url, database, etc. But it show me an error like this :

Message: Undefined property: Home::$home

Filename: controllers/Home.php

Line Number: 11

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: 11

Backtrace:

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


Here is my controller look like :

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

Here is my 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 */

And here is MY_Controller.php and MY_Model.php in my core folder, because i made a custom configuration like this :

<?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: Di deklarasikan dalam masing-masing model
     * 
     * @return void
     */
    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();
    }


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


    public function where($column, $conditions)
    {
        $this->db->where($column, $conditions);
        return $this;
    }


    public function like($column, $condition)
    {
        $this->db->like($column, $condition);
        return $this;
    }


    public function orlike($column, $condition)
    {
        $this->db->or_like($column, $condition);
        return $this;
    }


    public function join($table, $type = 'left')
    {
        $this->db->join($table, "$this->table.id_$table = $table.id", $type);
        return $this;
    }


    public function orderBy($column, $order = 'asc')
    {
        $this->db->order_by($column, $order);
        return $this;
    }


    public function first()
    {
        return $this->db->get($this->table)->row();
    }


    public function get()
    {
        return $this->db->get($this->table)->result();
    }


    public function count()
    {
        return $this->db->count_all_results($this->table);
    }


    // CRUD Model
    public function create($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function update($data)
    {
        return $this->db->update($this->table, $data);
    }

    public function delete()
    {
        $this->db->delete($this->table);
        return $this->db->affected_rows();
    }
    // End of CRUD Model


    // Pagination Model
    public function paginate($page)
    {
        $this->db->limit(
            $this->perPage,
            $this->calculateRealOffset($page)
        );

        return $this;
    }

    public function calculateRealOffset($page)
    {
        if (is_null($page) || empty($page)) {
            $offset = 0;
        } else {
            $offset = ($page * $this->perPage) - $this->perPage;
        }

        return $offset;
    }

    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  justify-content-center">',
            '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'         => '<i class="fa fa-angle-left" aria-hidden="true"></i>',
            'prev_tag_open'     => '<li class="page-item">',
            'prev_tag_close'    => '</li>',
            'next_link'         => '<i class="fa fa-angle-right" aria-hidden="true"></i>',
            '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"></span></a></li>',
            'num_tag_open'      => '<li class="page-item">',
            'num_tag_close'     => '</li>',

        ];

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


/* End of file MY_Model.php */

Could you guys please explain to me, why i got error like this? FYI. In my local server the code is working well.

Upvotes: 1

Views: 1709

Answers (1)

Maroof
Maroof

Reputation: 208

Not definitely sure about your server. The server that I deploy my applications on throws an error when I include more than 1 uppercase in Filename, controller class name, or model class name.

try replacing your "My_Controller" to "My_controller".

Upvotes: 1

Related Questions