首页 > 解决方案 > 添加代码以从另一个控制器运行

问题描述

在 CodeIgniter 框架中,我有两个控制器文件:controllerA.php 和 controllerB.php

我需要controllerB.php 在controllerA.php 函数中添加代码

我不知道该怎么做,我检查了 Codeigniter 手册、google 和 stackoverflow 但无法找到解决方案

controllerA.php 具有以下功能:

function get_permission_conditions()
{
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

]);
}

我希望 controllerB.php 与 controllerA.php 通信并添加自定义代码示例:

function get_permission_conditions()
{

//Code from controllerA.php
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

//custom code from controllerB.php goes here

]);
}

标签: phpcodeigniter

解决方案


你应该像这样扩展controllerA

class controllerA extends CI_Controller{
    // All the function will go here
    return do_action('staff_permissions_conditions', [
        'contracts' => [
            'view'     => true,
            'view_own' => true,
            'edit'     => true,
            'create'   => true,
            'delete'   => true,

    ]); 
}
class controllerB extends controllerA{
    public $permission_array;
    function __construct() {
        $this->permission_array = $this->do_action();  // Here $permission_array will have the array returned by controllerA's function 'do_action'
    }

    //custom code from controllerB.php goes here
    // You can use $permission all over
}

推荐阅读