How to Inherit A Model from Another Model in CodeIgniter - codeigniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.

core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');

Put the file genesis_model.php in the core directory

Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.

You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)

other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Related

CI_Template' not found

I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).

Codeigniter 3 url route with independent controllers

I have created a web application with Codeigniter, and I have a problem at the url, controller and structure level.
I have the following web structure.
http://projectroot/admin
Then I have several sections like:
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
I'm working with sessions and other libraries
Currently I have everything in a single controller called Admin, but I would like to create independent controllers that would be calling each part of the url.
In Admin I have:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
if($this->session->userdata('login')){
if($this->session->userdata('urlnow')){
$url = $this->session->userdata('urlnow');
redirect($url);
}else{
redirect('admin/index');
}
}else{
$data = array();
$data['usererror'] = $this->session->flashdata('usererror');
$data['passerror'] = $this->session->flashdata('passerror');
$data['message'] = $this->session->flashdata('message');
$this->load->view('admin/index', $data);
}
}
...
public function users() {
code....
}
public function profile() {
code....
}
public function section_tracking() {
code....
}
public function section_products() {
code....
}
My idea is that the controller folder contains something like this:
admin.php
users.php
profile.php
section_products.php
section_visits.php
Creating independent Admin extends classes (user, profile, section_tracking and section_products) as independent controllers outside of admin, with a structure similar to this:
users.php
class Users extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
profile.php
class Profile extends Admin {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('My_PHPMailer');
$this->load->library('session');
}
public function index(){
code here...
}
}
How can I do it? I don't want to use HMVC, I just want with MVC native.
Thank you
In your application/controllers/admin folder...Make following files
Admin.php
Users.php
Profile.php
Section_products.php
Section_visits.php
Then make call to each files like this...
http://projectroot/admin/users
http://projectroot/admin/profile
http://projectroot/admin/section_tracking
http://projectroot/admin/section_products
etc...
And here you want to extends admin controller.so make Admin_controller.php by extending CI_Controller in application/core. Your `Admin' controller must be like this....
class Admin_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Then always extends Admin_controller.CI_Controller automatically extended.

CodeIgniter - Replace redunant JSON conversion

I recently started using Codeigniter after having a structural problem in one of my Ajax-heavy applications. (You can read up on it if you want in my previous question)
I have a fairly short question. Currently I am making a lot of Ajax requests to different controllers. I open the controllers like this:
public function __construct()
{
parent::__construct();
$this->output->set_content_type('application/json');
}
And at the end of every function I do the following:
$this->returnValue['result'] = "ReturnedInfo";
$this->returnValue = json_encode($this->returnValue);
$this->output->set_output($this->returnValue);
The code is pretty clear in itself, but I don't want to keep repeating myself. The codeigniter manual says to do the following:
$this->output
->set_content_type('application/json')
->set_output(json_encode(array('foo' => 'bar')));
But I would still be repeating myself. Also, I don't want to add a function to every controller that does this, even if it does decrease redundancy.
Since all of my controllers return JSON, is there a way to set this globally in a config file maybe, or in any other way?
TL;DR I have this same piece of code in every controller/function. Since the output type is always the same, just not the result, is there a way to automate this process across every controller/function?
Create an Ajax_Controller that extends MY_Controller that extends CI_Controller.
The Ajax Controller will then inherit from both Controllers.
class Ajax_Controller extends MY_Controller
{
public function __construct()
{
parent::__construct();
if(!$this->input->is_ajax_request()) return show_error('Invalid Request');
}
public function jsonOutput($json)
{
//some data checking here....
return $this->output
->set_content_type('application/json')
->set_header("HTTP/1.1 200 OK")
->set_output($json);
}
}
-
class User extends Ajax_Controller
{
public function __construct()
{
parent::__construct();
}
public function userMethod()
{
$json = json_encode(array(
'' => ''
));
$this->jsonOutput($json);
}
}
Extend your controllers from your own base class rather than CI_Controller and put your repeatedly-used function(s) and constructor code in there. Something like:
class BaseController extends CI_Controller {
protected function index() {
$this->returnValue['result'] = "ReturnedInfo";
$this->returnValue = json_encode($this->returnValue);
$this->output->set_output($this->returnValue);
}
}
class Specific extends BaseController {
public function index() {
//do controller-specific stuff
parent::index();
}
}
I abstract this further if I have groups of controllers with shared code; for example, if I had a bunch of controllers that require the user to be logged-in I create AuthenticatedController, which extends BaseController and add session checks etc.

Autoload libraries in Codeigniter

I am a newbie to Codeigniter.
I have 3 libraries in autoload in config.php .
But in one of my controllers I don't want to load the libraries. Is this possible?
If you need any library throughout the application you can load it in the config file and it will be auto loaded. But if you need a library only in a specific controller you can load it in the controller where you need it.
Class test Extends CI_Controller{
function index()
{
$this->load->library('mylibrary');
$this->mylibrary->somemethod();
}
}
Or if you need library through out the controller you can load it in the constructor.
Class test Extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->library('mylibrary');
}
function index(){
$this->mylibrary->somemethod();
}
function test(){
$this->mylibrary->someothermethod();
}
}
Extend CI_Controller in your libraries.
Something like this:
class MyLibrary extends CI_Controller {
var $ci;
function __construct() {
$this->ci = &get_instance();
$route = $this->ci->router->fetch_class();
if( $route == strtolower('YourController') ) return false;
}
}
you can remove libraries from autoload file. then they will not be activ in the framwork.
If you want to use them, you can call them in constructors if you want them in a class. If you want to use them in just method, you load them in the method.

Loading codeigniter view with predefined variables

I do not know if the following is possible. If not other suggestions are appreciated.
In nearly all my controllers I will load some default views. For example header, footer and menu.
I would like to have certain variables auto load for each view.
If I take the header as an example. Into my header I will always load an array of $css scripts and an array of $javascript files.
$javascript[] = 'js/jquery.js';
$javascript[] = 'js/jqueryui.js';
But additionally, depending on the current page logic, I might want to add another javascript file to my $javascript variable.
$javascript[] = 'js/custom.js';
Then ideally, I would like these variables to be automatically inserted as data into the load of the view.
In other words, I just want to call:
$this->load->view('header');
How could this be achieved?
Create MY_Controller add a public array there then extend from MY_Controller
class MY_Controller extends CI_Controller {
public $data;
function __construct() {
parent::__construct();
$this->data['MYVAR'] = 'Something';
}
}
in your other controllers you just do it like this
class SomeClass extends MY_Controller {
function __construct () {
parent::__construct();
}
function index () {
$this->data['SomeOtherVar'] = 'xxx';
$this->load->view('viewname', $this->data);
}
}
You can use $this->load->vars in your Controller.
I use this in my_controller and all controllers are extend from MY_Controller
For example
<?php
class MY_Controller extends Controller{
public function __construct(){
parent::__construct();
$this->setGlobalViewVariables();
}
public function setGlobalViewVariables(){
$result = array();
$result['value1'] = 'value1';
$result['value2'] = 'value1';
$this->load->vars($result);
}
}
?>
you should create an hook for this, it is very simple

Resources