php 如何在codeigniter的另一个控制器中调用一个控制器功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21110197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call one controller function in another controller in codeigniter
提问by user2936213
I have one controller named home.phpin which a function named podetailsis there. I want to call this function in another controller user.php.
Is it possible to do so? I have read about HMVCin CI, but I want to know is it possible to do without using hmvc?
我有一个名为的控制器home.php,其中有一个名为的函数podetails。我想在另一个控制器中调用这个函数user.php。
有可能这样做吗?我HMVC在 CI 中读过,但我想知道是否可以不使用 hmvc?
回答by Kyslik
To extend controller please either follow this tutorialor see some code below.
要扩展控制器,请遵循本教程或查看下面的一些代码。
differences between private/public/protected
私有/公共/受保护之间的差异
make a file in folder /application/core/named MY_Controller.php
在/application/core/名为的文件夹中创建一个文件MY_Controller.php
Within that file have some code like
在该文件中有一些代码,如
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Controller extends CI_Controller {
protected $data = Array(); //protected variables goes here its declaration
function __construct() {
parent::__construct();
$this->output->enable_profiler(FALSE); // I keep this here so I dont have to manualy edit each controller to see profiler or not
$this->load->model('some_model'); //this can be also done in autoload...
//load helpers and everything here like form_helper etc
}
protected function protectedOne() {
}
public function publicOne() {
}
private function _privateOne() {
}
protected function render($view_file) {
$this->load->view('header_view');
if ($this->_is_admin()) $this->load->view('admin_menu_view');
$this->load->view($view_file . '_view', $this->data); //note all my view files are named <name>_view.php
$this->load->view('footer_view');
}
private function _isAdmin() {
return TRUE;
}
}
and now in any of yours existing controllers just edit 1st or 2nd line where
现在在您现有的任何控制器中只需编辑第一行或第二行
class <controller_name> extends MY_Controller {
and you are done
你就完成了
also note that all your variables that are meant to be used in view are in this variable (array) $this->data
还要注意,所有打算在视图中使用的变量都在这个变量中 (array) $this->data
example of some controller that is extended by MY_Controller
由以下扩展的某些控制器的示例 MY_Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class About extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['today'] = date('Y-m-d'); //in view it will be $today;
$this->render('page/about_us'); //calling common function declared in MY_Controller
}
}
回答by ReNiSh A R
write the podetails()as a function within a helper file.
将podetails()编写为帮助文件中的函数。
then load that helper in both of the controllers.
然后在两个控制器中加载该助手。
in the controller you just call podetails()
在控制器中,您只需调用 podtails()
Suppose:
认为:
--controller 1--
--控制器1--
function podetails()
{
podetails(); // will call function in helper ;
}
--controller 2--
--控制器2--
function podetails()
{
podetails(); // will call function in helper ;
}

