helper is not loading in Codeigniter model - codeigniter

<?php
class MyModel extends CI_Model {
public function loadData()
{
$CI =& get_instance();
$CI->load->helper('data_helper');
print_r($CI->data_helper); //this is printing nothing
$CI->data_helper->loaditems(); // method is not calling
}
}
function loaditems()
{
echo "hello from load of helper";
}
?>
helper filename is data_helper.php
give me you thought about this why it not working and in which case it will work

Put the file data_helper.php in the /application/helpers directory.
In /application/config/autoload.php load the helper using just the word 'data'. (line 92).
$autoload['helper'] = array('data');
Or you can load it before you need it with $this->load->helper('data');
Then you can use loaditems() from anywhere like a normal function.
You don't need the $CI magic at all.

According to the documentation
$this->load->helper('name');
Where name is the file name of the helper, without the .php file extension or the “helper” part.
which means the following code should work
class MyModel extends CI_Model
{
public function loadData()
{
$this->load->helper('data');
loaditems();
}
}
you can read more about it here

Try data_helper() to call helper function.
data_helper();

Related

How do I load a model method into a controller constructor in CodeIgniter?

I am trying to extend the CI_Controller class to load my global page header file so I don't have to load it at the beginning of every single controller method. It doesn't seem to be working. I know the Controller extension itself works... if I remove the call of the model method from the constructor and load it from my controller method, the rest of the controller extension works fine. But when I load the model method from within the constructor of the controller extension, I get a blank page(I haven't generated the main content yet).
Any ideas?
application/core/MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
var $user = array();
function __construct(){
parent::__construct();
$this->load->model('member');
if($this->session->userdata('member_id')){
$this->member->get_info($this->session->userdata('member_id'));
$this->user = $this->member->info;
$this->member->update_activity($this->session->userdata('member_id'));
} else {
$this->load->helper('cookie');
if(get_cookie('Teacher Tools Member Cookie')){
$this->member->auto_login(get_cookie('Teacher Tools Member Cookie'));
} else {
$this->user = $this->member->default_info();
}
}
$this->load->model('template');
$this->template->overall_header();
}
}
application/models/template.php
<?php
class Template extends MY_Model {
function __construct(){
parent::__construct();
}
function overall_header($title = 'Home'){
$data = array(
'BASE_URL' => base_url(),
'MAIN_NAVIGATION' => $this->main_navigation(),
'TOOLBAR' => $this->toolbar()
);
return $this->parser->parse('overall_header.tpl', $data);
}
MY_Model is an extension of the CI_Model class to load member information into $this->user.
I think response generation is done in Controller's method and all HTML pieces that you might have gets glued there. So if you are calling /controller/method_a then method_a will be responsible for returning response whereas in constructor you cannot set response.
I agree with you on setting important data in Constructor once so that you don't have to do this again and again in each method. I think you should assign output to some Controller level variable and then use that variable in your Controller's method.
I' am sure you got my point.
For this reason there is a config/autoload.php:
$autoload['model'] = array('YourModel');

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

Accessing Variables from MY_Controller CodeIgniter

As I have read through articles, the best possible way to validate a user in all controllers of my application is to extend my own Controller that extends CI_Controller
But I can't make it work.
What I have is a members_controller that extends my MY_Controller.
in MY_Controller i have this piece of code, just to test if how am I going to utilize this.
<?php
class MY_Controller extends CI_Controller {
private $foo;
function __construct() {
parent::__construct();
$this->foo = 'hello world again';
}
}
in my members controller
<?php
class Members extends MY_Controller {
function index() {
$this->load->view('members');
}
}
in my members view
i want to access this variable i just set up, but i don't know how.
i tried echoing it out like this echo $foo, like this $this->foo
but neither work. thanks much!!
You need to pass the variable to your view file.
<?php
class Members extends MY_Controller {
function index() {
$data = array('foo' => $this->foo)
$this->load->view('members', $data);
}
}
Now you can refer to $foo inside your view. You may need to declare $this->foo as protected.
There's a video tutorial on MY_Controller that you may find useful at http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond
Have you tried passing the variable to the 'members view'.
$this->load->view('members',$foo);
You need to make $foo as protected and not private.
Then you will be able to access it in your controller that extends MY_Controller.php like this:
$this->foo
Today for the first time I used codeigniter 3. In the past I used version 2.x a lot of times. Now I can't extend CI_Controller:
application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
private $data = array();
function __construct() {
parent::__construct();
date_default_timezone_set("Europe/Zagreb");
$this->data['foo'] = "bar";
}
application/controllers/Welcome.php
class Welcome extends MY_Controller {
public function index() {
$this->data['main_content'] = 'home';
$this->load->view('template/template', $this->data);
}
application/view/home.php
(this is view loaded by $this->data['main_content'])
If I try to echo any data declared in MY_Controller, I get an error.
<?php echo $foo; ?>
A PHP ERROR WAS ENCOUNTERED
Severity: Notice
Message: Undefined variable: foo
Filename: template/header.php

How to Inherit A Model from Another Model in 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';
}
}
?>

Resources