Undefined property: Auth::$model [CodeIgniter] - php

I know this quention might seems duplicated, but i already tried those suggestions/advice but still couldn't solve this:
environment :
$autoload['libraries'] = array('email', 'session', 'database', 'form_validation');
controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
$this->load->model('Auth_model', 'Auth');
}
private function _login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$user = $this->model->Auth->getUser();
Model :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth_model extends CI_Model
{
public function getUser()
{
$email = $this->input->post('email');
// var_dump($email);die;
return $this->db->get_where('t_user', ['email' => $email])->row_array();
}
}
I Still got this error :
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Auth::$model
Filename: controllers/Auth.php
Line Number: 38

Change
$user = $this->model->Auth->getUser();
into
$user = $this->Auth->getUser();

try this into the class you will use
Type $ this-> library -> (" library you want "). I hope it helps.

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

Undefined property: Login_Controller::$user

Why do I get this error? I Already Load database in libraries too,
but it's giving error
Undefined property: Login_Controller::$user
I'm stuck for making login validation
This Is The Picture
This is my autoload
$autoload['model'] = array();
$autoload['helper'] = array('url');
$autoload['libraries'] = array('database', 'session','form_validation');
this is my Login_Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->view('login');
}
function verify()
{
$this->load->model('user_model');
$check = $this->user->validate();
//$this->load->library('database');
if($check)
{
$this->load->view('admin/main');
}
else
{
$this->load->view('login');
$message = "Username and/or Password incorrect.\\nTry again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
And This Is My Model
class User_model extends CI_Model
{
public function __construct() {
parent::__construct();
}
function validate()
{
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
$this->db->get_where('user',$array)->result();
}
}
Thanks For The Help
Change this line
$check = $this->user->validate();
to
$check = $this->user_model->validate();
Update the validate() function as follows
function validate(){
$array['username'] = $this->input->post('username');
$array['password'] = $this->input->post('password');
return $this->db->get_where('user',$array)->num_rows();
}
Update $this->load->model('user_model'); To $this->load->model('user_model', 'user');
and update line $this->db->get_where('user',$array)->result(); to return $this->db->get_where('user',$array)->result(); in User_model validate method.
When you load user model $this->load->model('user_model'); by default it will assign User_model class object to user_model variable in Login_Controller. Which you can access as $this->user_model.
So when you access User_model class method validate using $this->user->validate(); you are getting error Undefined property: Login_Controller::$user.
If you want to use user variable pass 2nd parameter to load mode method as bellow:
$this->load->model('user_model', 'user');

Call to undefined method in Codeigniter

I am creating a model there is no error in my case, but it give me error
Message: Call to undefined method Auth_model::chech_user()
Here is my code
Controller
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$params = array(
'iteration_count_log2' => '8',
'portable_hashes' => TRUE
);
$this->load->library('hash', $params);
$this->load->model('auth_model');
}
public function login()
{
if (isset($_POST) && !empty($_POST)) {
print_r($this->auth_model->chech_user($_POST));
exit;
}
$this->load->view('auth/login');
$this->load->view('layouts/footer');
}
Model
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth_model extends CI_Model
{
public function check_user($params = array())
{
echo 'done';
}
}
There's a typo within the code you've provided, you're trying to call chech_user() not check_user() within the controller.
change this line
print_r($this->auth_model->chech_user($_POST));
to
print_r($this->auth_model->check_user($_POST));

controller loding error in codignater?

<?php
session_start();
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class View_job extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Userdb', '', TRUE);
$this->load->model('Locationdb');
$this->load->model('All_functiondb');
$this->load->library('form_validation');
}
}
///
A PHP Error was encountered Severity: Notice
Message: Undefined property: Indexpg::$session
Filename: php_include/authenticate.php
Line Number: 3
Fatal error: Call to a member function userdata() on null in
C:\xampp\htdocs\CodeIgniter-job-site-master\hindusthanjobs\application_candidate_6423\php_include\authenticate.php
on line 3
define session after tis line
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
first thing is that check is session library is loded in controller.
public function __construct()
{
parent::__construct();
$this->load->library('session');
}

Codeigniter Session is working on localhost but not on web server

I am trying to use session in my project. It works on localhost but not on web server. I read some articles about that in stackoverflow and ellislab, but it didn't work for my project. Probably problem is about creating ancillary classes.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
$CI =& get_instance();
parent::__construct();
$CI->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$CI->load->helper('date');
$CI->load->helper('ip_address');
$CI->load->model('Sql_model');
}
Now errors are :
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Line Number: 14
and
A PHP Error was encountered
Severity: Error
Message: Call to a member function library() on a non-object
Line Number: 14
Try this code,
function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
If class is in controllers directory, you can directly get the CI instance by extending the CI_Controller and use $this.
Than, try without calling get_instance() function. Just like docs says:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Memberlogin extends CI_Controller {
//protected $CI;
function __construct()
{
parent::__construct();
$this->load->library('session');
//$this->load->library('form_validation');
//$this->load->helper(array('form', 'url'));
$this->load->helper('date');
$this->load->helper('ip_address');
$this->load->model('Sql_model');
}
This will make you crazy. but it works.
In autoload.php
$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','date','ip_address');
and load model in __constructor
public function __construct()
{
parent::__construct();
$this->load->model('Sql_model');
}

Categories