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 */
Related
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 have one controller named home.php in which a function named podetails is there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVC in CI, but I want to know is it possible to do without using hmvc?
To extend controller please either follow this tutorial or see some code below.
differences between private/public/protected
make a file in folder /application/core/ named MY_Controller.php
Within that file have some code like
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
class <controller_name> extends MY_Controller {
and you are done
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
example of some controller that is extended by MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
write the podetails() as a function within a helper file.
then load that helper in both of the controllers.
in the controller you just call podetails()
Suppose:
--controller 1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
function podetails()
{
podetails(); // will call function in helper ;
}
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
Let's say we have module called core_crud with something like this in the controller:
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mdl_core_crud');
}
public function index()
{
// code goes here
}
}
And now I want to extend this module with another module called shop_crud. How would the basic controller for this shop_crud module look like? I mean I want to inherit all the controller methods from core_crud and all the model stuff too.
Structure of the Modules
/modules
/core_crud
/controllers
/core_crud.php
/models
/views
/shop_curd
/controllers
/shop_crud.php
/models
/views
Code in core_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('mdl_core_crud');
}
public function index()
{
// code goes here
}
public function mymethod($param1 = '', $param2 = '')
{
return 'Hello, I am called with paramaters' . $param1 . ' and ' . $param2;
}
}
Code in shop_crud.php
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Shop_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
//$this->load->model('mdl_shop_curd');
}
public function testmethod()
{
// output directly
$this->load->controller('core_crud/mymethod', array('hello', 'world'));
// capture the output in variables
$myvar = $this->load->controller('core_crud/mymethod', array('hello', 'world'), TRUE);
}
}
So instead of extending the whole module/controller I prefer just to call the method which is required. It is simple and easy too.
Note If module name and controller name are different then you have to pass the path
module_name/controller_name/mymethod
EDIT to support EXTENDS
File structure
The code in core_crud.php.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Core_crud extends MX_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('core_crud/mdl_core_crud');
}
public function index()
{
return 'index';
}
public function check_method($param1 = '')
{
return 'I am from controller core_crud. ' . $this->mdl_core_crud->hello_model() . ' Param is ' . $param1;
}
}
The code in mdl_core_crud.php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class mdl_core_crud extends CI_Model
{
public function hello_model()
{
return 'I am from model mdl_core_crud.';
}
}
The code in shop_crud.php.
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once APPPATH . '/modules/core_crud/controllers/core_crud.php';
class Shop_crud extends Core_crud
{
public function __construct()
{
parent::__construct();
}
public function index()
{
echo parent::check_method('Working.');
}
}
Output :- I am from controller core_crud. I am from model
mdl_core_crud. Param is Working.
Hope this helps. Thanks!!
If you are loading the models in the parent class or in the construct then it should be inherited in shop_crud. are you not looking to do class Shop_crud extends Core_crud {? is parent::__construct() not retaining the construct for you?
Is this something you can handle with routing to the same controller rather than extending a controller (wanting to inherit both the controller and the model seems strange to me or something you could handle with a route and a private function in the class to handle the logic)?
"Controllers" this name defines it's functionality. The controller is used to control a particular section. So in MVC framework I think it's better to create individual controller for individual module. But you can reuse the model i.e. you can call one model's function in another model. For this
First load your model like $this->load->model("modelName"); in your controller
Then call the function like $this->modelname->functionName();
From what I can gather, you have to require the parent controller that you are extending. This isn't exactly ideal, but I'll look into a better way to do this later on. For now, I've created a simple function to do the inclusion.
function extend_module($module) {
$path = realpath(APPPATH) . '/modules/'. $module.'/controllers/'.ucfirst($module).'.php';
require_once($path);
}
Usage:
extend_module('some_module');
class othe_ module extends some_module {
NOTE: The function needs to be available outside of the CI object, so put it somewhere like your main index.php file.
Also note: As these variables are used to reference the local file system, do not dynamically assign them directly from user generated input. Doing so would cause multiple file system vulnerabilities.
Platform: CI3 + Bonfire 8 HMVC