简体   繁体   English

从两个不同的控制器中调用Codeigniter 3的方法

[英]Method Calling in Codeigniter 3 from two different controllers

I am using CodeIgniter 3 . 我正在使用CodeIgniter 3

Here's my Admin controller where I insert a record into the table people : 这是我的Admin控制器,我在其中将记录插入到表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. 这是我执行与Admin控制器相同的操作的另一个控制器,但是它两次写入相同的代码。 I want to reuse the code in the Employee controller: 我想重用Employee控制器中的代码:

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. 我发现的另一种方法是在Employee redirect(../admin/addpeole) ,但它更改了URL。

Is there any another way to reuse same code in the Admin controller in the Employee controller without changing the URL? 还有其他方法可以在不更改URL的情况下在Employee控制器的Admin控制器中重用相同的代码吗?

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 您可以在application/models模型下创建application/models您必须给它起一个不与控制器冲突的名称,因此大多数人都会添加后缀_model_m

eg: application/models/Employees_model.php 例如: application/models/Employees_model.php

it should extends CI_Model the code would be something like this. 它应该扩展CI_Model,代码将是这样的。

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 然后在您的控制器中加载模型并向其发送$ data以创建一名员工

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. 尝试此操作在核心中创建一个名为MY_Controller.php的文件,并从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. 扩展Employees形成在核心中创建的控制器。

class Employee extends Admin_controller{
       public function __construct() {
           parent::__construct();

       }

    function abc() {
         $this->addpeople();// this will insert the values for you
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM