Method Calling in Codeigniter 3 from two different controllers - php

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
}
}

Related

How to access sibling controller function in PHP Codeigniter

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');
}

Access an array in all views in codeigniter

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.

CodeIgniter call protected method of another controller

I have two classes on my Codeigniter project: Users and Profiles
Users:
class Users extends CI_Controller
{
...
Profiles:
class Profiles extends CI_Controller
{
protected function create_user_profile()
{
....
}
...
When a user is created by user controller, a profile should be created immediately. So a function in Users has to call create_user_profile function. Now my question is:
If I make create_user_profile public, one can call it by URL. But if I keep it protected, then how to call it from User controller?
Is there a better way than moving create_user_profile from Profiles controller to Users controller?
Try making a Profiles Library instead :
Libraries/profiles.php
class Profiles
{
protected $CI;
public function __construct()
{
$this->CI =& get_instance(); // Existing Code Igniter Instance
}
public function create_user_profile()
{
// Your Code Here
// can communicate back with CI by using $this->CI
// $this->CI->load->view(....);
// $this->CI->load->model(...);
// ETC
}
}
controllers/users.php
class Users extends CI_Controller
{
public function my_function(){
$this->load->library('profiles');
$this->profiles->create_user_profile();
}
}
Put it in a different, non-web-controller class. Then you can re-use it from everywhere.

Unable to locate CodeIgniter model

I'm trying out codeigniter for the first time on a MAMP deployment however I'm unable to load a model into a controller. The end result is always...
Unable to locate the model you have specified: second_model
Controller - welcome.php
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('second_model');
$this->load->view('welcome_message');
}
}
Model - second_model.php
class Second_model extends Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}
You should be extending CI_Model rather than Model
So:
class Second_model extends CI_Model {
public function __construct()
{
// model constructor
parent::__construct();
}
}

How to create two parent controllers in Codeigniter?

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();
}
}

Categories