Codeigniter - undefined property error calling model from controller file - php

I have an error with the following file, and specifically, I assume, the following line:
$data['events'] = $this->homes->get_events();
The error is:
Message: Undefined property: CI_Loader::$homes
I am in the controller file, where I assume the problem is.
File name: /application/controllers/Home.php
$data['events'] = $this->homes->get_events(); <?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function _construct()
{
parent::_construct();
$this->load->model('homes');
}
public function index()
{
$data['events'] = $this->homes->get_events();
$this->load->view('template/header.php');
$this->load->view('home/index.php', $data);
$this->load->view('template/footer.php');
}
}
There is a model file at models/Homes
We tried changing the name of the model file to 'homes' (lower case) and also the code to 'Homes', but the same error persisted.
Specifically the error on running the url is:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$homes
Filename: controllers/Home.php
Line Number: 15
Backtrace:
File: public_html/anchor/application/controllers/Home.php
Line: 15
Function: _error_handler
File: /public_html/anchor/index.php
Line: 315
Function: require_once

Related

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

I'm about to launch a website which I built with codeigniter3. I've made configuration changes such as base_url, database, etc. But it shows me an error like this :
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 ```
So, here is my controller :
<?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 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);
}
}
If I'm running the program in my localhost, it's totally fine and works. Why do I get this error on a production server?
From the error message you received, the important part is
Message: Call to a member function select() on null
which means your model was not loaded!
as your code works on your localhost environment, but not on the production server, most likely the error is caused by applying wrong Naming Conventions:
File names must be capitalized. For example: Myclass.php Class
declarations must be capitalized. For example: class Myclass Class
names and file names must match.
public function __construct(){
parent::__construct()
}
add this to your Home controller

Message: Undefined property: Todo::$uri

I use codeigniter.
I get an error when I try to get the parameter in the url
<?php
class Todo extends CI_Controller{
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('index.php');
}
public function blog(){
$this->load->view('arsiv');
}
public function test($param){
$this->load->view('test');
$a = $this->uri->segment(2);
}
}
when i try to go : http://localhost/cod/index.php/todo/test/123
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Todo::$uri
Filename: controllers/Todo.php
Line Number: 21
Backtrace:
File: /opt/lampp/htdocs/cod/application/controllers/Todo.php
Line: 21
Function: _error_handler
File: /opt/lampp/htdocs/cod/index.php
Line: 315
Function: require_once
An uncaught Exception was encountered
Type: Error
Message: Call to a member function segment() on null
Filename: /opt/lampp/htdocs/cod/application/controllers/Todo.php
Line Number: 21
Backtrace:
File: /opt/lampp/htdocs/cod/index.php
Line: 315
Function: require_once
Please verify the URL helper is enabled in your config function
try to use $this->uri->get_segment(1);
Try to use
$CI =& get_instance();
$this->CI->uri->segment('2')
you have to load helper in codigniter autoload.php file in config folder
$autoload['helper'] = array('url');
Try to use
codigniter autoload.php file in config folder
$autoload['helper'] = array('url');
Url : http://localhost/laravel/users/abc
$product_id = $this->uri->segment(2);
Result : abc
Read https://www.codeigniter.com/user_guide/libraries/uri.html

Unable to use controller "Error" for 404_override routing

I have controller named "Error" in codeigniter "application/controllers/Error.php".
I used this controller to override default 404 error page through routes.
$route['404_override'] = 'myerror';
But on 404 error occurrence I am getting following error.
A PHP Error was encountered
Severity: Warning
Message: call_user_func_array() expects parameter 1 to be a valid callback, class 'Error' does not have a method 'index'
Filename: core/CodeIgniter.php
Line Number: 514
Backtrace:
File: C:\xampp\htdocs\ci-app\index.php
Line: 292
Function: require_once
But when I rename Error controller to another, it works.
Please help.
You need to change your controller and file name to myerror like,
<?php
//application/controllers/Myerror.php
class Myerror extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->output->set_status_header('404');
$data['content'] = 'error_404'; // Let View name
$this->load->view('index',$data);// loading view
}
}

Search form in CodeIgniter

I am having a problem while creating a search form in CodeIgniter - it's showing errors like:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Login::$membership_model
Filename: controllers/login.php
Line Number: 12
Backtrace:
File: C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php
Line: 12
Function: _error_handler
File: C:\xampp\htdocs\Project5_1\CodeIgniter\index.php
Line: 315
Function: require_once
Fatal error: Call to a member function getSearchBook() on null in C:\xampp\htdocs\Project5_1\CodeIgniter\application\controllers\login.php on line 12
A PHP Error was encountered
Severity: Error
Message: Call to a member function getSearchBook() on null
Filename: controllers/login.php
Line Number: 12
Backtrace:
My Controller Page is:
<?php
class Login extends CI_Controller{
function search(){
$searchBook = $this->input->post('searchBook');
$data = $this->membership_model->getSearchBook($seachBook);
$this->load->view('main_content',$data);
}
?>
My Model page is:
class Membership_model extends CI_Model{
function getSearchBook($searchBook) {
$this->db->select('*');
$this->db->from('book');
$this->db->like('author',$searchBook);
return $query->result();
}
}
My main_content.php is:
<?php
echo form_open('login/search');
echo form_input('searchBook','Search Book', 'id="searchBook"');
echo form_submit('submit', 'Search');
echo form_close();
?>
<table><th>Title</th></table>
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['title'];
}
?>
</div>
</body>
It's been a long time since I used CodeIgniter, but from what I remember, you're not loading the model in your Login class constructor. That's why you're getting these two parts of the error:
Message: Undefined property: Login::$membership_model
Message: Call to a member function getSearchBook() on null
Neither the property or the function exist in the Login class if you haven't loaded the membership_model model.
Off the top of my head, I think it needs to be something like this in your Login controller:
<?php
class Login extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model('membership_model');
}
function search(){
$searchBook = $this->input->post('searchBook');
$data = $this->membership_model->getSearchBook($seachBook);
$this->load->view('main_content',$data);
}
?>

Controller - Model in CodeIgniter and "Undefined property"

<?php
class Success_model extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
//insert into user table
function get_all()
{
$query = $this->db->get('session'); // = select * from session
return $query->result();
}
}
<?php
class Success extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
$this->load->model('success_model');
}
public function index()
{
$data= $this->Success_model->get_all();
$this->load->view('success_view', $data);
}
}
?>
As You can see this is good, but I have error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Success::$Success_model
Filename: controllers/success.php
Line Number: 15
Backtrace:
File: C:\xampp\htdocs\session\application\controllers\success.php
Line: 15
Function: _error_handler
File: C:\xampp\htdocs\session\index.php
Line: 292
Function: require_once
Fatal error: Call to a member function get_all() on null in C:\xampp\htdocs\session\application\controllers\success.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to a member function get_all() on null
Filename: controllers/success.php
Line Number: 15
Backtrace:
I'm looking, and looking, but I dont know what is bad. The good directories are files, other classes are working but this not.
Please help. I'm just tired, because after 2 hours searching mistakes...
For CI v3, afaik
I think I see it:
$data= $this->Success_model->get_all();
Should be:
$data = $this->success_model->get_all(); // Notice the lowercase success_model
PHP variables are case-sensitive where as functions and class names are not.
One more solution would be to change
$this->load->model('success_model');
into
$this->load->model('Success_model');
because then you can use
$data= $this->Success_model->get_all();

Categories