I have a custom helper that i use for logging.
Within one of the functions of the helper i need to get the name of the controller that was called. Is there a way to do it?
I can't rely on uri segments because some controllers are in sub-folders and the helper is used all over.
You can use the following in CI2.x
$this->router->fetch_class();
You may need to get an instance of the CI super variable $this first- in which case. Use the following:
$ci =& get_instance();
$ci->router->fetch_class();
There's also a $ci->router->fetch_method(); method if you need the name of the method called for any reason.
$this->>router->fetch_method(); will return index if you do something like this:
class Someclass extends CI_Controller {
function index(){
$this->edit();
}
function edit(){
$this->router->fetch_method(); //outputs index
}
}
this should work (not so sure if it works in the helper):
$ci =& get_instance();
$ci->router->class // gets class name (controller)
$ci->router->method // gets function name (controller function)
You can also use the URI class
$ci = & get_instance();
$ci->uri->segment(1) // That stands for controller
$ci->uri->segment(2) // That stands for method
Related
<?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();
I have 2 different model files with one common function.
So I written that function in another model file trying to access that by using:
$this->load->model('model_1');
$this->model_1->com_fun();
But it is not working. How can I call the functions of one model class inside another model class?
Sometimes is useful to have in your controllers something like:
public function __construct(){
$this->CI =& get_instance();
}
So, when you need another model you can do:
$this->CI->load->model('another_codeigniter_model_name', 'another');
$something = $this->CI->another->com_fun();
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');
When I try to call
$this->load->database();
yields the following error `"Call to a member function database() on a non-object"
Autoloading the database doesnt help too...
when I try to autoload it.
all calls to database like
$this->db->get('people');
it says get method is undefined...
I have no clue what and where to start..\
anyone ?
Go to autoload.php in application/config/autoload.php and add this
$autoload['libraries'] = array('database'); // add database in array
Make sure your connection settings are fine in application/config/database.php
Than in the library do it like this
Class MyLib
{
function getPeople(){
$CI = &get_instance();
$query = $CI->db->get('people');
return $query->result();
}
}
Use extends CI_Model if not working try extends Model
class User_model extends CI_Model {
public function __construct()
{
parent::__construct();
$this->load->database();
}
}
You can load the database by two methods:
Method 1:Automatic Connecting
$autoload['libraries']=array('database');
Method 2: Manual Connecting
$this->load>database();
i hope above methods clear your confusion....
You are doing a very common mistake. When you call $this->load->database(); from controller or model it works because controllers and models are child of CI_Controller and CI_Model respectively. But when you are call them from Library which is not a child class of any basic CIclass you cannot load database() or anything else using $this-> key. you must use the help of &get_instance(); to load codeigniter instance and use that instance instead of $this. Which suggests following Code:
$INST=&get_instance();//Store instance in a variable.
$INST->load->database();//If autoload not used.
$INST->db->get('people');//or Your desired database operation.
It is better to keep a field variable to hold the reference to $INSTas you may need to access it in various functions.
Following Code will be more eligent:
class MyLib{
var $INST;
public function __construct()
{
$INST=&get_instance();//Store instance in a variable.
$INST->load->database();//If autoload not used.
}
function getPeople(){
$query = $INST->db->get('people');
return $query->result();
}
}
When I want to access a function of a user defined model in CodeIgniter inside a custom user defined library it throws
Call to a member function Set_where() on a non-object
Although I load the model by using this inside that library
$CI =& get_instance();
$CI->load->model('home_model');
And I'm using this code to access the function inside home_model class
$CI->home_model->Set_where("film_feature='Y'");
So now it throws the above error.
So could someone please help me solve this error?
You could try to instantiate your model object like:
include_once('path/modelname.php');
$home_model = new Home_model();
$home_model->set_where(....);
make sure your model extends de default CI model class.
You could also use $CI->load->model(modelname); instead of include... It's easier to deal with path problems, however it would be a little less efficient because load->model will instantiate an object.
Now i fix the issue just adding $CI =& get_instance(); into that function where i want to fetch previously i declare that $CI =& get_instance(); in another function of that class for which it was not instantiated.
My working code is now..
$CI =& get_instance();
$CI->load->model('home_model');
$CI->home_model->Set_where("film_feature='Y'");