extending core controller on codeigniter - php

I have placed this controller on application/core, called MY_base.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Base_Controller extends CI_Controller {
public function __construct(){
parent::__construct();
}
function set_spaces($post_array, $primary_key){
$post_array['nombre']=str_replace(" ", "_", $post_array['nombre']);
return $post_array;
}
}
?>
and this controller on the controllers folder
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Clase extends Base_Controller {
function __construct(){
parent::__construct();
$this->load->library('grocery_CRUD');
}
function index(){
if($this->session->userdata('logged_in')){
$this->grocery_crud->set_table('clase');
$this->grocery_crud->set_subject('Clase');
$this->grocery_crud->required_fields('nombre');
$this->grocery_crud->unset_columns('id');
$this->grocery_crud->unset_print();
$this->grocery_crud->unset_export();
$this->grocery_crud->unset_add();
$this->grocery_crud->callback_before_update(array($this,'set_spaces'));
//$this->grocery_crud->set_theme('datatables');
$output = $this->grocery_crud->render();
//$this->load->view('header', $data);
$this->load->view('header2.php',$output);
$this->load->view('clase_view', $output);
}
else{
redirect('login', 'refresh');
}
}
}
?>
I think I've followed documentation's instructions correctly but I get this error
Fatal error: Class 'Base_Controller' not found in C:\xampp\htdocs\esi3d\application\controllers\clase.php on line 3
It looks like is looking for base_controler on controllers folder not in core folder.

Place your MY_base.php also in controllers folder

Related

Codeigniter: Pass data from model to controller but didn't get in the view file

I was returning query->result() from my model to controller and in the controller I passed the $data to the view file but I am not getting any data in the view file.
Here is the show_model.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
function get_post_data()
{
$query=$this->db->get('posts');
return $query->result();
}
}
Here is the show.php file:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Show extends CI_controller {
public function __construct()
{
parent::__construct();
}
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('list_view',$data);
}
}
Here is the show_list.php view file:
<div>
<?php echo "<pre>"; print_r($records); exit; ?>
</div>
The name of your view is show_list and in the controller you are using list_view
simply replace the code in the controller for displaying the view with
function show_post_data(){
$this->load->model("Show_model");
$data['records'] = $this->Show_model->get_post_data();
$this->load->view('show_list',$data);
}

Codeigniter error shows in my homepage call to a member function on null

I just started my website developing using the CodeIgniter framework.
My working steps are given below:
1. I created a controller named by Home.php
2. Then I go to the view folder and created a file named by index.php under sub-directory 'home'.
3. When I browse my website it shows me an error
Fatal error: Call to a member function view() on null in /home/ontutors/public_html/application/controllers/Home.php on line 18
Home.php code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
}
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>
Overwrite your code like this.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller
{
function index()
{
//echo "Hello welcome";
$this->load->view('home/index');
}
}
?>

Undefined property: CI_Loader::$impostazioni_model

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";
}
}

Hit Counter in Codeigniter

I have the code below:
(Step by step)
Put counter.txt in APPPATH . 'logs/counter.txt'
Make counter_helper.php set in APPPATH . 'helpers/counter_helper.php';
Autoload newly created helper in APPPATH . 'config/autoload.php' file;
Make MY_Controller.php in APPPATH . 'core/MY_Controller.php'
Any controller should extend MY_Controller instead of CI_Controller;
Echo it on page with: <?php echo $this->count_visitor;?>
The Helper :
<?php defined('BASEPATH') OR exit('No direct script access allowed.');
if ( ! function_exists('count_visitor')) {
function count_visitor()
{
$filecounter=(APPPATH . 'logs/counter.txt');
$kunjungan=file($filecounter);
$kunjungan[0]++;
$file=fopen($filecounter, 'w');
fputs($file, $kunjungan[0]);
fclose($file);
return $kunjungan[0];
}
}
The Core :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $count_visitor;
public function __construct()
{
parent::__construct();
$this->count_visitor = count_visitor();
}
}
/* End of file MY_Controller.php */
/* Location: ./application/core/MY_Controller.php */
The Controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
public function index() {
$data=array('isi' =>'home/index_home');
$this->load->view('layout/wrapper',$data);
}
}
The View :
<?php echo $this->count_visitor;?>
The code return an error like below :
I got it to work fine when I loaded the helper $this->load->helper('counter');
application > core > MY_Controller.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public $count_visitor;
public function __construct()
{
parent::__construct();
$this->load->helper('counter');
$this->count_visitor = count_visitor();
}
}
Yes, must load the helper:
$this->load->helper('counter');
or
config/autoload.php: $autoload['helper'] = array('counter');

CodeIgniter 'MY_' can not be found in ... error

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 */

Categories