Inheritance controller in codeigniter 3 - php

I have a lot of controllers in controller's folder like this :
app
controller (folder)
-> admin (folder)
-> C_admin_eir
-> C_admin_eir_first_revisi
How can I make the class C_admin_eir_first_revisi is a inheritance from C_admin_eir
I create like this :
C_admin_eir
<?php
namespace App\controllers;
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class C_admin_eir extends Admin_Controller {
public $data = array(
'halaman' => 'home',
'main_view' => 'admin/v_home'
);
}
C_admin_eir_first_revisi
namespace App\controllers;
use App\controllers\C_admin_eir;
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class C_admin_eir_first_revisi extends C_admin_eir {
public $data = array(
'halaman' => 'First Revisi',
'main_view' => 'admin/v_home_revisi'
);
/* SOME OVERRIDE FUNCTION*/
}
Error : Fatal error: Class 'App\controllers\C_admin_eir'
Thanks for the help

You can achieve your goal by moving your controllers to the global namespace. Here is an example:
Parent_ctrl.php
class Parent_ctrl extends CI_Controller
{
public function parent_method()
{
echo 'I am parent_method';
}
}
Child_ctrl.php
require APPPATH.'controllers/Parent_ctrl.php';
class Child_ctrl extends Parent_ctrl
{
public function index()
{
$this->parent_method();
}
}

Related

Unable to locate the specified class in codeigniter Session.php

I created a hook in codeigniter framework, but it show me Unable to locate the specified class: Session.php.
How can I fix this?
Here is my hook that I created
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$hook['pre_controller'] = array(
'class' => 'Demo',
'function' => 'test',
'filename' => 'Demo.php',
'filepath' => 'hooks',
);
Here is the Class that i call from the hook
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Demo extends CI_Controller {
public function test() {
echo 'it works';
}
}
a hook cannot extend CI_Controller, especially pre_controller. remove extends CI_Controller

I get " {"status":false,"error":"false"} " in codignater .. why?

I have an error in my code ,, I'm writing code in php framework ` codeignater ,, i want to extend REST_Controller but i can't , my code not working clearly,, please help me ,, here is my error and my code ,
{ "status": false, "error": false }
controller / Auth.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//require('libraries\REST_Controller.php');
require APPPATH . '/libraries/REST_Controller.php';
//require('libraries\Format.php');
class Auth extends REST_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('api_model');
// $this->load->library('rest_controller_lang');
}
function data_post()
{
$data = array(
'name' => $_POST['name'],
'surename' => $_POST['surename'],
'email' => $_POST['email'],
'password' => $_POST['password'],
);
$this->api_model->insert($data);
$this->response("my first api");
var_dump($data);
}
}
?>
models/Api_model.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Api_model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function insert($data){
if($this->db->insert('peoples', $data)){
return FALSE;
}
return TRUE;
}
}
?>
config/autoload.php
$autoload['libraries'] = array('database','REST_Controller');
Drag and drop the application/libraries/Format.php and application/libraries/REST_Controller.php files into your application's directories.Copy the rest.php into your ** application/config** directory.
For the code try referencing the controller directly.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require(APPPATH . 'libraries/REST_Controller.php');
class Auth extends \Restserver\Libraries\REST_Controller
{......}
I have added namespace Restserver\Libraries; in my REST_CONTROLLER
<?php
namespace Restserver\Libraries;
use Exception;
use stdClass;
defined('BASEPATH') OR exit('No direct script access allowed');
abstract class REST_Controller extends \CI_Controller
{......}
For the response try sending an HTTP Code along with it
$this->response("my first api", 200);

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');

inherit code in codeigniter php

Im trying to inherit a constant var which are in another file/class. But I tried almost everything and I can't do it for some reason, can anyone tell me what is wrong or just are happening in my code please?
this is my main class that i called BasePage.
class BasePage extends CI_Controller {
const $TITLE = "My Own DB";
}
and this is the other class/file which is trying to inherit to other class by giving the constant var TITLE into an array.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require 'BasePage.php';
class Home extends BasePage {
public function index()
{
$data = array('title' => $TITLE);
$this->load->helper('url');
$this->load->view('page_login', $data);
}
}
The constant should not have a $ at the beginning.
And you don't access it like $TITLE, or just TITLE, but with self::TITLE. In this case it would be most correct to use parent::TITLE, since it belongs to a parent class.
This should work:
class BasePage extends CI_Controller {
const TITLE = "My Own DB";
}
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require 'BasePage.php';
class Home extends BasePage {
public function index()
{
$data = array('title' => parent::TITLE);
$this->load->helper('url');
$this->load->view('page_login', $data);
}
}

Custom Controller being called when it shouldn't be

Using CodeIgniter, I have a few custom controllers:
MY_Controller - this extends the CI_Controller
Front_Controller - extednds MY_Controller, used for any "user" facing pages
Admin_Controller - extends MY_Controller, used for "admin" pages
Now, when I am on the admin "index" page, the controller of which uses Admin_Controller, I am getting logged errors coming from the Front_Controller.
I put a var_dump() into the Front_Controller and I cannot see it on the admin page, so I'm confident it is not being called by my code, but somehow, it does seem to be getting called!
Is there any explanation for this?
Some code:
Admin_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Admin_Controller extends MY_Controller
{
public function __construct() {
parent::__construct();
if(!$this->ion_auth->logged_in()) {
redirect('login', 'location');
} elseif($this->ion_auth->logged_in() AND !$this->ion_auth->in_group(array('admin', 'mod'))) {
redirect('account/dashboard', 'location');
} else {
$this->user = $this->ion_auth->user()->row();
}
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'admin/inc/nav',
'flash' => 'inc/flash_messages',
'user' => $this->user
);
}
}
Admin index:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Index extends Admin_Controller {
public function __construct() {
parent::__construct();
}
public function index()
{
$this->load->vars($this->data);
$this->load->view('admin/index');
}
}
MY_Controller
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->data = array(
'head' => 'inc/head',
'foot' => 'inc/foot',
'nav' => 'inc/nav',
'flash' => 'inc/flash_messages',
);
$this->load->model('new/resellers/reseller_model');
$this->load->model('new/white_labels/white_label_model');
// find matching domain and show logo
$domain = $_SERVER['SERVER_NAME'];
$reseller = $this->reseller_model->get_by('domain', $domain);
if($reseller):
$resellerWhiteLabel = $this->white_label_model->get($reseller->white_label_id);
if($resellerWhiteLabel):
$this->data['portal_reseller'] = $reseller;
$this->data['portal_reseller_white_label'] = $resellerWhiteLabel;
endif;
endif;
$this->load->library('lib_log');
$this->load->library('ion_auth');
$this->load->helper('language');
$this->lang->load('auth');
$this->lang->load('site');
}
}
As far as I can remember CodeIgniter is a singleton, so it loads everything on each request.
This means that the Front_Controller will get loaded even if not called. So if there are errors in it they will out.
Take some time, fix the errors and all should be good.

Categories