I have the following controller structure:
with the following code:
MAIN CONTROLLER:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load_defaults();
}
public function load_defaults() {
}
}
CHILD CONTROLLER 1:
class Child1 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 1";
}
}
CHILD CONTROLLER 2:
class Child2 extends MY_Controller {
public function __construct() {
parent::__construct();
$this->main();
}
public function main() {
echo "function in Child Controller 2";
}
}
My question: How do I call a function located in Child1 controller, from the Child2 controller?
If you have to call a controller from another controller, you're doing it wrong. Controllers are there to accept URI requests from the client.
Please try to revisit the problem and see if you can move the common logic to MY_Controller - it will be accessible because all other controllers extending it.
Also a model will be good place to have common functions that will be called from controllers.
I think what you are looking for is redirect. Considering that by CI convention each controller function should be an endpoint for the user, if you want to access a function from another controller then what you are really trying to do is redirect them to that controller.
function someEndPoint(){
$this->load->helper('url');
redirect('someOtherEndPoint', 'refresh');
}
Related
contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.
Suppose I have two child controllers of MY_Controller,say Child_1 & Child_2.In MY_Controller I have two methods method_1 & method_2 as
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
some code ...
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I am calling method_2() from child_1 & child_2 using
class Child_1 extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
public function method_1(){//custom implementation goes here}
public function some_method()
{
...some code
$this->method_2();//call inherited method method_2()
}
}
A similar code for child_2
abstract class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public abstract function method_1();//children will give custom implementation
public function method_2()
{
// some code ...
$this->method_1(); //Call the method directly it will implement the child's method
/*Here I want to call method_1() of the child controller that have called this method i.e. method_2* automatically */
}
}
I am using CodeIgniter 3.
Here's my Admin controller where I insert a record into the table people:
class Admin extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
This is another controller where I perform the same operation as the Admin controller, but it writes the same code twice. I want to reuse the code in the Employee controller:
class Employee extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
Another way I found is to redirect(../admin/addpeole) in Employee, but it changes the URL.
Is there any another way to reuse same code in the Admin controller in the Employee controller without changing the URL?
Controllers should not communicate with the database directly, You should be using Models to communicate with the database, And your Controllers should be communicating with models then sending the output either directly or through a view.
You can create a model under application/models You'll have to give it a name that does not conflict with controllers so most people add the suffix _model or _m
e.g: application/models/Employees_model.php
it should extends CI_Model the code would be something like this.
class Employees_model extends CI_Model {
public function add_employee($data) {
// insert code goes here
}
]
then in your controller you load the model & send it the $data to create an employee
class Employees extends CI_Controller {
public function create () {
$this->load->model('employees_model');
$this->employees_model->add_employee($this->input->post());
}
}
try this
In core create a file called MY_Controller.php and extend from CI_Controller.
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function addpeople() {
insert in into people values('','pramod','emp0784');
}
}
?>
Write the function in that controller
Extend Employees form the controller created in core.
class Employee extends Admin_controller{
public function __construct() {
parent::__construct();
}
function abc() {
$this->addpeople();// this will insert the values for you
}
}
I made an array in MY_Controller class placed on core folder. In its constructor i fetched records from db so as to make navigation menu in my views. Since i have different page layouts so i cannot call the same header view every where. for this reason i made a core class as per my understanding which i am not sure is right or not. below is the code for my controller
class MY_controller extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$data['parent'] = $this->Category_model->getParentCategories();
$data['child'] = $this->Category_model->getChildCategories();
}
}
my default controller is main
class Main extends MY_controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('home/header',$data);
$this->load->view('home/footer');
}
Now in my header view i am receiving undefined variable parent and child error. I want this two variables available in all the views so that i do not have to define those two variables in every controller.
Thanks
You may try something like this:
class MY_controller extends CI_Controller
{
$commonData = array();
function __construct()
{
parent::__construct();
$this->load->model('Category_model');
$this->commonData['parent'] = $this->Category_model->getParentCategories();
$this->commonData['child'] = $this->Category_model->getChildCategories();
}
}
Then use $this->comonData in your index method instead of $data to pass to the view:
public function index()
{
$this->load->view('home/header', $this->comonData);
$this->load->view('home/footer');
}
Now it'll be available in the header view and since it's at the top of other views then you may use it further, unless you override it with other value in any class.
I want to create two parent controllers: one for admin and one for user site. They have to extend a regular Controller class but each of them has to do different things.
I wrote up an article showing how you do this.
http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY
You need to create an __autoload() function in your config.php or directly include the base controller above the class definition.
This is pretty easy. Do the following:
Go to the following directory: your_ci_app/application/core/ and create a php file called MY_Controller.php (this file will be where your top parent classes will reside)
Open MY_Controller.php and add your multiple classes, like so:
class Admin_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test() {
var_dump("from Admin_Parent");
}
}
class User_Parent extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
var_dump("from User_Parent");
}
}
Create your children controllers under this directory your_ci_app/application/controllers/ . I will call it adminchild.php
Open adminchild.php and create your controller code, make sure to extend the name of the parent class, like so:
class Adminchild extends Admin_Parent {
function __construct() {
parent::__construct();
}
function test() {
parent::test();
}
}