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();
}
}
Related
I am about trying to learn about Codeigniter,
here in this try and error project, i want to call function from another class.
Here is the detailed code:
controller/admin/dashboard
class Admin extends CI_Controller{
public function dashboard() {
$this->load->library("overview");
$this->overview->method_overview();
}
}
Where, the overview is a the filename of Overview class that i want to call and the method_overview is a function inside overview class.
Here the overview.php
class Overview extends CI_Controller
{
public function method_overview() {
}
}
And this is the error that i got:
Unable to load the requested class: overview
Can somebody please give me solution or explanation?
Overview class inherit the property of CI_Controller you just inherit Overview class
So it would be
class Admin extends Overview{
public function dashboard() {
$this->load->library("overview");
$this->method_overview();
}
}
class Overview extends CI_Controller
{
public function method_overview() {
}
}
Since you're calling the overview class as a library, save that class as Overview.phpinside application/libraries.
In that file, something similar to:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Overview {
function __construct() {
$this->CI =& get_instance(); //gives access to the CI object
}
public function method_overview() {
//....
}
}
I have a function within an AdminController for sending emails. I want to access this within another controller. Can anyone advise how I would modify this to work?
OrdersController
public function postOrder()
{
$order = New Order;
...
$order->save();
// email order (call function in other controller)
$this->emailOrder($order);
}
AdminController
public function emailOrder($order)
{
//email processing goes here
}
You would strip it out, either into an abstract controller, that your controllers inherit from:
class AdminController extends MyController
Or to a service that you can call from your controller:
Mail::sendOrder($order)
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
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.
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
}
}