php点击按钮加载控制器,PHP-CodeIgniter:在con中加载控制器

使用以下代码,您可以加载控制器类并执行方法。

该代码是为codeigniter 2.1编写的

首先在application / core目录中添加一个新文件MY_Loader.php。 将以下代码添加到新创建的MY_Loader.php文件中:

// written by AJ sirderno@yahoo.com

class MY_Loader extends CI_Loader

{

protected $_my_controller_paths = array();

protected $_my_controllers = array();

public function __construct()

{

parent::__construct();

$this->_my_controller_paths = array(APPPATH);

}

public function controller($controller, $name = '', $db_conn = FALSE)

{

if (is_array($controller))

{

foreach ($controller as $babe)

{

$this->controller($babe);

}

return;

}

if ($controller == '')

{

return;

}

$path = '';

// Is the controller in a sub-folder? If so, parse out the filename and path.

if (($last_slash = strrpos($controller, '/')) !== FALSE)

{

// The path is in front of the last slash

$path = substr($controller, 0, $last_slash + 1);

// And the controller name behind it

$controller = substr($controller, $last_slash + 1);

}

if ($name == '')

{

$name = $controller;

}

if (in_array($name, $this->_my_controllers, TRUE))

{

return;

}

$CI =& get_instance();

if (isset($CI->$name))

{

show_error('The controller name you are loading is the name of a resource that is already being used: '.$name);

}

$controller = strtolower($controller);

foreach ($this->_my_controller_paths as $mod_path)

{

if ( ! file_exists($mod_path.'controllers/'.$path.$controller.'.php'))

{

continue;

}

if ($db_conn !== FALSE AND ! class_exists('CI_DB'))

{

if ($db_conn === TRUE)

{

$db_conn = '';

}

$CI->load->database($db_conn, FALSE, TRUE);

}

if ( ! class_exists('CI_Controller'))

{

load_class('Controller', 'core');

}

require_once($mod_path.'controllers/'.$path.$controller.'.php');

$controller = ucfirst($controller);

$CI->$name = new $controller();

$this->_my_controllers[] = $name;

return;

}

// couldn't find the controller

show_error('Unable to locate the controller you have specified: '.$controller);

}

}

现在,您可以将所有控制器加载到application / controllers目录中。例如:

加载控制器类发票并执行功能test()

$this->load->controller('invoice','invoice_controller');

$this->invoice_controller->test();

或当类在目录中时

$this->load->controller('/dir/invoice','invoice_controller');

$this->invoice_controller->test();

就像加载模型一样

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值