我有一个命名的控制器home.php
,其中有一个命名的函数podetails
。我想在另一个控制器中调用此函数user.php
。
有可能这样做吗?我已经阅读过HMVC
CI,但是我想知道是否可以不使用hmvc而做?
要扩展控制器,请遵循本教程或在下面查看一些代码。
私人/公共/受保护者之间的差异
在/application/core/
名为的文件夹中创建文件MY_Controller.php
在该文件中包含一些代码,例如
<?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;
}
}
现在在您现有的任何控制器中,只需编辑第一行或第二行
class <controller_name> extends MY_Controller {
你完成了
还请注意,您打算在视图中使用的所有变量都在此变量中 (array) $this->data
通过扩展的某些控制器的示例 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
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句