This question already has answers here:
Extending The Controller Class in CodeIgniter
(5 answers)
Closed 4 years ago.
I am new to codeIgniter and facing issue while extending a controller. I do know there is answer on stackoverflow but it is relevant to extending a core class and does not provide me solution. As given below, I want to extend 'Auth_controller' but doing so gives me class not found error
MY Auth_controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
abstract class Auth_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
if(!$this->session->userdata('u_id')){
return redirect(base_url('admin/login'));
}
else
{
if(!$this->login_model->do_check_login($this->session->u_id,$this->session->email,$this->session->session_id,$this->session->role_id))
{
return redirect(base_url('admin/login'));
}
echo "login successful";
}
}
public abstract function index();
}
My Dashboar_controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Dashboard_controller extends Auth_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('email');
$this->load->library('calendar');
}
public function index()
{
//my code here
}
}
error
Fatal error: Class 'Auth_Controller' not found in C:\xampp1\htdocs\Admin\application\controllers\admin\Dashboard_controller.php on line 4
You probably need to add require_once('Auth_controller.php') at the top of Dashboard_controller.php; otherwise the Dashboard file will not know about the Auth class. (See require_once in the php docs).
https://stackoverflow.com/a/27910751/594235
Related
I am trying to user the Codeigniter pagination and I receive the following error "Fatal error: Call to undefined method Pagination::initialize()".
I found a few post with the same issue but they all stated the solution is to auto load the Pagination class which I am already doing. The only issue I can think of is that my class uses custom controller "MY_Controller" which extends the CI controller. Below is my code, please help. Thanks in advance.
MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->init();
}
}
Main Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends MY_Controller {
function index()
{
$this->display();
}
function display()
{
//pagination settings
$config['base_url'] = base_url();
$config['total_rows'] = 200;
$config['per_page'] = 50;
$this->pagination->initialize($config);
}
}
If you made a custom controller and extend it with core My_Controller than load pagination library inside the display() function:
$this->load->library('pagination');
I'm working on a calendar planning provided by [easyappointments][1] but now I'm stuck on this error by yesterday. In the main page users.php I've added the following:
<?php
require_once("application/models/impostazioni_model.php");
$this->load->model('impostazioni_model');
$this->impostazioni_model->load_colours();
?>
the require_once find correctly the file impostazioni_model.php but when I enter in the page users.php I see this error:
Fatal error: Call to a member function load_colours() on a non-object
on this line: $this->impostazioni_model->load_colours();
in the class impostazioni_model.php I've this content:
<?php if ( ! defined('BASEPATH')) exit('Direct execution not allowed.');
class Impostazioni_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function load_colours()
{
echo "print";
}
}
?>
I've followed the codeigniter documentation, in particular the class model must be in capital letter so I don't know what I'm wrong. Someone could help me out?
In Controller
Path - application/controllers
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('impostazioni_model'); //this will show "Print" word on browser.
}
function index() {
$this->impostazioni_model->load_colours();
}
}
In Model
Path - application/model
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Impostazioni_model extends CI_Model {
function __construct() {
parent::__construct();
}
function load_colours() {
echo "print";
}
}
I create simple library called Xauth.php to check if user already login or not:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Xauth
{
protected $ci;
public function __construct()
{
$this->ci =& get_instance();
}
public function is_logged_in()
{
if ($this->ci->session->userdata('is_logged_in'))
{
return true;
}
return false;
}
}
I put that library in my Admin_Controller, so whatever controller extended with Admin_Controller will be checked first, if the session data is empty they will be redirect to login page. And this is my Admin_Controller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Admin_Controller extends MY_Controller {
public function __construct()
{
$this->load->library('Xauth');
if ($this->Xauth->is_logged_in() == false) {
redirect('auth');
}
}
}
But I got an errors, it says:
Message: Undefined property: Dashboard::$Xauth
Where is my fault?
You must use your class with lowercase letters :
$this->xauth->is_logged_in()
I am fairly new to Codeigniter and am trying to call in a function from my model but I cannot get it to work. Can anyone see what I am doing wrong here?
Controller (farm.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Farm extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('harvest_first');
}
function harvest()
{
echo $this->harvest_first->harvest();
}
}
Model (harvest_first.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Harvest_first extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function harvest(){
return "THE FUNCTION";
}
}
?>
I am trying to echo "THE FUNCTION", but no matter what I do I cannot get it to work as expected.
Thanks,
Simon
Try this
class Harvest_first extends CI_Model
change to :
class Harvest_first_model extends CI_Model
and in controller call like this:
$this->load->model('harvest_first_model');
and
$this->harvest_first_model->harvest();
Check here
There is no need to add "_model" to model that depends on you
just Load the Model and that its use autoload.php and add model there it is a good pratices
I have been working on a session validation for my login to make sure that a user is logged in to view pages. I keep getting this error:
Fatal error: Class 'MY_Staffcontroller' not found in /usr/local/var/www/CodeTest
/ci/application/controllers/staff_c.php on line 3
My staff_c page looks like so :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Staff_c extends MY_Staffcontroller {
function homepage()
{
$data['main_content'] = 'homepage_view';
$this->load->view('includes/template', $data);
}
}
I have been reading same questions all over the place and they say the same thing pretty much...
Is your controller located in application/core?
Well yes it is. I can't seem to get passed this hump!
This is the code within My_Staffcontroller.php:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_staffcontroller extends CI_Controller {
function __construct()
{
parent::__construct();
$loggedin = $this->session->userdata('loggedin');
if(!isset($loggedin) || $loggedin != TRUE);
{
die($this->load->view('denied'));
}
}
}
I know this is user error as this is only my second day with CodeIgniter but I can't seem to find proper workaround for this?
I have tried this tutorial and still nothing and also this
Even following this video has me stuck on the session part.
And I just can not get this to work.
Remember Linux is case-sensative whereas Windows is case-insensative.
place you're MY_Staffcontroller inside application/core/MY_Controller.php file
Your MY_Controller.php file should look like this (minus all you're other functions, this is a minimal example)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
}
class MY_Staffcontroller extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function sayHello()
{
echo "Hello, I am a function within MY_Staffcontroller.php";
}
}
Example
This will be located in /application/controllers directory
Basically any protected and public functions located in either MY_Controller OR MY_Staffcontroller will be accessible from derived controllers that extend the extended controller. In this case it would be MY_Staffcontroller
class Public_Staff_Controller extends MY_Staffcontroller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->sayHello();
}
}
/* end of file /application/core/MY_Controller.php */