php - 添加代码以从另一个控制器运行
问题描述
在 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
]);
}
解决方案
你应该像这样扩展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
}
推荐阅读
- amazon-web-services - 使用 aws chime 增加 createMediaCapturePipeline 的视频捕获时间
- python - 重命名熊猫系列中的索引
- server-side-rendering - SvelteKit 未从 IONICONS 加载图标
- c# - 如何以一种形式发布多个用户?
- python - 在 Python 中删除一维列表中的重复项 - 仅适用于某些项目
- email - 电源 BI。SMTP 地址没有与之关联的邮箱,Microsoft Exchange
- javascript - 有没有办法为调用 html 元素并对其执行某些操作的 javascript 函数编写单元测试?
- bash - 将值分配给包含变量的变量的 shell 脚本
- r - 如何执行引导程序以找到 R 中 k-nn 模型的置信区间?
- javascript - 在 node.js 应用程序中应该在哪里写 res.send()