Where should one instantiate a global object in Code Igniter? - codeigniter

I want to create a global object that represents the current user in Code Igniter. The constructor of the object takes the user id, which is stored in $_SESSION['user_id'].
Every time a user visits a page, I want to create this user object. Where should I instantiate it? I think instantiating it in config/constants.php works, but is there a more standard/reliable place to instantiate it?

One option is to create a MY_Controller from which all your other controllers would inherit. The user object could be instantiated within MY_Controller and therefore would be available in every controller which inherits from it.
Simplified example:
class MY_Controller extends CI_Controller {
public $user;
function __construct(){
parent::__construct();
// Get the current user (pseudo code, obviously)
$this->user = $this->user_model->get_user($id);
}
}
class Some_other_controller extends My_Controller {
function __construct(){
parent::__construct();
// $user is available throughout this controller
}
}
class Another_controller extends My_Controller {
function __construct(){
parent::__construct();
// $user is available throughout this controller
}
}

Related

How to create multiple MX Controller (Base controllers) in codeigniter 2.2.2

im trying to create a base controller for some controllers.
Im extending MY_Controller from CI_Controller on application/core. I have a folder named "ajax" in application controllers and i pretend to create a parent class and extend from ajax/classes. Extending all from MY_Controller.
Some like this
class Ajax extends MY_Controller{
public function __construct(){
blablabla
}
}
class Ajax extends MY_AJAX{
public function __construct(){
blablabla
}
}
class user extends Ajax{
blablabla
}
Is it necessary to use includes, load_class, etc. or are there another "legal" way to do that?
Regards!
SOLVED YET:
How to create two parent controllers in Codeigniter?
This works for me:
class MY_Ajax extends MY_Controller{
public function __construct(){
CI_Controller::__construct();
}
}

Calling other controller methods from a controller failed in Codeigniter

I want to call upload_controllers methods in admin controller. I've attached my code here. But its not working. Rather its shows Unable to locate the specified class: Session.php
class Admin_controller extends CI_Controller {
public function __construct() {
parent::__construct();
this->load->helper('url');
}
function index(){
require_once(APPPATH.'controllers/uploads_controller.php');
$testObj = new uploads_Controller();
$testObj->index();
}
}

how do i extend a class in codeigniter?

this is my controller: common :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
and this the second controller (register):
class Register extends Common {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
when i access the user_registration function it shows me this error:
Fatal error: Class 'Common' not found in /home/attilana/domains/attila-naghi.com/public_html/application/controllers/register.php on line 3
How do i access the test() function from the class common , in the user_registration function from the class register?
In Codeigniter calling a controller from another controller even in an example like this is not possible because of the situation that it doesn't allow multiple controller instances in a request. Because of this situation you have three main options actually.
First, you can extend Codeigniter's base controller, CI_Controller, with MY_ prefix so that Codeigniter will recognize it as a common base class. The main idea here is to write the methods that will be common in most of your controllers.
If we take a look at your example and try to adapt it as mentioned above :
Initially, you need to extend the CI_Controller class:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
Secondly, create your register controller as below:
class Register extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
Another possibility is you can create a helper and call that function globally or you can create a library and load it within your controller and call that.
For further information, you can read from here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html or from old docs: https://github.com/EllisLab/CodeIgniter/wiki/MY-Controller

Can't access public properties from MY_controller when extending from MX_Controller in HMVC

I have implemented Codeigniter HMVC which works fine. Then I'm extending MX_Controller with MY_Controller to share functionality and properties between all my modules and to keep my code DRY.
But I noticed that while extending MY_Controller from MX_Controller I can no longer access the public properties from MY_Controller on any of the extended child classes.
Some sample code:
class MY_Controller extends MX_Controller {
public $variable;
function __contruct()
{
parent::__contruct();
$this->variable = 'Foo';
}
}
Then on any controller which I extend from MY_Controller:
class Foo extends MY_Controller {
function __construct()
{
parent::__construct();
}
function someFunction()
{
var_dump($this->variable);
}
}
When I trying to access the public property $variable in any child controller I get Null or empty string.
I have searched high and low for this with no luck, my only guess is an issue with HMVC MX_Controller. Any Ideas?
After hours of checking, it was a simple mistake on my behalf. I had misspelled the name parent::__contruct on MY_Controller, it should be parent:__construct.

CodeIgniter call same function for all controllers

I'm developing an application, where the same function I'm writing for some controllers. So I wanted one common function for all controllers. Please help me out I've stucked up with this. The work would be more appreciated.
You can develop a helper or library.
codeigniter version 2 : https://codeigniter.com/userguide2/general/creating_libraries.html
codeigniter version 3 : https://codeigniter.com/user_guide/general/creating_libraries.html?highlight=library
You can create MY_Controller class in application/core
Class MY_Controller Extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function common(){
// code here
}
}
Now extend every class with MY_Controller and the function
will be available for each class
Class Test Extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
When in the url you call
http://localhost/app/test/common
This will work.
create a model class and create that all function after that extend that model all model classes .you can use one function different controller.other wise you can use Cms helper.this is the best way call a common function in different controller.

Resources