Loading model of codeigniter - codeigniter

I have a problem loading a model, i have code like this
function __construct() {
$this->load->model("admin_model");
parent::__construct();
}
function login() {
$log = $this->admin_model->login($username, $pass);
}
if i use the code above, i get this error
A PHP Error was encountered
Severity: Notice
Message: Undefined property: admin_controller::$admin_model
Filename: controllers/admin_controller.php
Line Number: 43
Fatal error: Call to a member function admin_login() on a non-object in C:\xampp\htdocs\ci\application\controllers\admin_controller.php on line 43

Put this code on top of your function parent::__construct();

Related

Disable ion auth for front end in codeigniter

I've been trying to accomplish this for some time but no results..
I'm using a ion auth in my codeignitor website.
I want to disable ion auth for front end login, and want it to only work for admin side login. is that possible? if yes then how..
my code:
class Login extends MY_Controller {
public function __construct(){
parent::__construct();
$this->load->library('session');
$this->load->helper('url');
require_once APPPATH.'vendor/autoload.php';
}
public function index(){
$this->render('login');
}
}
This generates below error:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI::$ion_auth
Filename: MX/Controller.php
Line Number: 59
Backtrace:
File: D:\xampp\htdocs\moneyclues2\application\third_party\MX\Controller.php
Line: 59
Function: _error_handler
File: D:\xampp\htdocs\moneyclues2\application\core\MY_Controller.php
Line: 126
Function: __get
File: D:\xampp\htdocs\moneyclues2\application\core\MY_Controller.php
Line: 59
Function: _setup
File: D:\xampp\htdocs\moneyclues2\application\controllers\Login.php
Line: 7
Function: __construct
File: D:\xampp\htdocs\moneyclues2\index.php
Line: 316
Function: require_once
when I extend CI_controller. I got below error for render method
Fatal error: Call to undefined method Login::render() in D:\xampp\htdocs\moneyclues2\application\controllers\Login.php on line 15
A PHP Error was encountered
Severity: Error
Message: Call to undefined method Login::render()
Filename: controllers/Login.php
Line Number: 15
Backtrace:
adding ion_auth library $autoload['libraries'] = array('database','ion_auth'); to autoload.php worked for me.
and extended MY_Controller in in place of CI_Controller.
You need to load the Ion_Auth library! $this->load->library('ion_auth');
But if you want it to only be in use for admin-related stuff just make two different MY controllers in core and have your public and admin controllers extend them. Don't autoload Ion_Auth if you only use it in 1/2 of your website.
class MY_Public_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load what you need here
}
}
class MY_Admin_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load what you need here
$this->load->database();
$this->load->library('session');
$this->load->library('ion_auth');
}
}
Example admin controller:
class Somecontroller extends MY_Admin_Controller {
public function index() {
}
}

How to fix this error: Call to a member function get() on a non-object?

Fatal error: Call to a member function get() on a non-object in
C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\Paging\application\models\name_model.php
Severity: Error
Message: Call to a member function get() on a non-object
Filename: models/name_model.php
Line Number: 13
Backtrace:
models/name_model.php:
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Line 13: $query = $this->db->get('name');
How to fix the error message?
You didn't load database libraries that why you getting this error.
Open your autoload.php file and add this string in libraries
$autoload['libraries'] = array('database');
And another way is you need to load libraries in the model
class Name_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_name()
{
$query = $this->db->get('name');
return $query->result();
}
}
Kindly write it as follow when your going to fetch only name(s) from your table,
public function get_name()
{
$query = $this->db->select('name')->get('your_table_name');
return $query->result();
}
It will work fine.

Codeigniter Getting an error of

I don't how to solve this error. While using the following controller and views in the CodeIgniter-3.0.0 Version and i change the baseurl also too http://localhost/code/
controllers:
Hello.php
<?php
class Hello extends CI_Controller {
var $name;
var $color;
function Hello()
{
$this->name = 'Andi';
$this->color = 'red';
parent::CI_Controller();
}
function you()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->view('you_view',$data);
}
}
?>
View:
you_view.php
Hello You!
<font color="<?=$color ?>"><?=$name?></font>
http://localhost/code/index.php/Hello/you
Got an error:
Fatal error: Call to undefined method CI_Controller::CI_Controller() in E:\phpprogram\code\application\controllers\hello.php on line 9
A PHP Error was encountered
Severity: Error
Message: Call to undefined method CI_Controller::CI_Controller()
Filename: controllers/hello.php
Line Number: 9
Backtrace:
You should extend CI_Controller not Controller in controllers/Hello.php
You need to replace Controller with CI_Controller.
Bookmark to your browser the user guide. It is something you will need to visit quite often. Most things are pretty well explained there.
Try this on your controller:
class Hello extends CI_Controller {
function hello()
{
parent::Controller();
}
function you()
{
$this->load->view('you_view');
}
}

Codeigniter: Extending CI_Exceptions not working in 2.2.1

I have an issue with my custom 404 page in codeigniter. The below code works fine in 2.2.0 but having upgraded to 2.2.1 i'm getting the following error:
Fatal error: Call to a member function helper() on a non-object in /application/core/MY_Exceptions.php on line 14
MY_Exceptions.php (/library/core/MY_Exceptions.php)
class MY_Exceptions extends CI_Exceptions {
public function __construct() {
parent::__construct();
}
public function show_404() {
$CI =& get_instance();
$CI->load->helper('form');
$CI->load->helper('text');
$CI->load->model('Pages_model');
$data['nav'] = $CI->Pages_model->getPages();
$CI->output->set_status_header('404');
$CI->load->view('header', $data);
$CI->load->view('error404', $data);
$CI->load->view('footer', $data);
echo $CI->output->get_output();
exit;
}
}
I thought maybe it should now be located in /application/libraries but when placed in here I just get the standard codeigintor 404 error page?
What am I missing?

Codeigniter HMVC Modular Error

I'm new to CI + MX and I tried the Modules::run(); style but I can't seem to let it work.
Here's my directory structure:
/application
-/modules
--/main
---/controllers
----main.php
---/models
---/views
--/connections
---/controllers
----connections.php
---/models
----/group_model.php
---/views
----connection_view.php
main.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MX_Controller {
function __construct(){
parent::__construct();
$this->load->helper('url');
}
function index(){
echo modules::run('connections/load_connections');
}
}
?>
connections.php controller:
<?php
class Connections extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->helper('url');
$this->load->model('connections/group_model');
}
function load_connections(){
$user_id = 2;
$data['tabs'] = $this->group_model->get_groups($user_id);
$this->load->view('connection_view', $data);
}
}
?>
group_model.php model:
class Group_model extends CI_Model{
function __construct(){
parent::__construct();
}
/**
* Get all groups in db
**/
function get_groups($user_id){
$this->db->select('g.group_name');
$this->db->from('groups AS g');
$this->db->join('members AS m', 'g.group_id = m.group_id');
$this->db->where('m.user_id', $user_id);
return $this->db->get()->result_array();
}
}
My connection_view.php view contains a div and some php codes to display the $data['tabs'] passed as array in load_connections function.
The problem is, I'm getting an error that says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Connections::$group_model
Filename: controllers/connections.php
Line Number: 14
and
Fatal error: Call to a member function get_groups() on a non-object in C:\xampp\htdocs\hmvc\application\modules\connections\controllers\connections.php on line 14
I have clearly followed all the instructions provided on MX wiki at https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
and set up everything as needed.
My database.php under /application/config is already configured.
routes.php is also configured pointing the default controller to main.php
I wonder what I have missed or have done wrong that I can't get it to work.
Codeigniter version: 2.1.3
MX version: 5.4
almost missed it, extend connections with MX_Controller since you are calling modules::run(). Whenever you want a controller to be called with modules::run(), you extend it with MX_Controller instead of CI_Controller
Your second error is caused because of your first error.
Your first error is most likely caused because it cant open the group_model.
try this
$this->load->model('group_model');

Resources