我有一个名为的控制器home.php,其中有一个名为的函数podetails.我想在另一个控制器中调用此函数user.php.
有可能这样做吗?我HMVC在CI中已经阅读过,但我想知道是否可以不使用hmvc?
Kys*_*lik 11
要扩展控制器,请遵循本教程或查看下面的代码.
私人/公共/受保护之间的差异
在/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;
}
}
Run Code Online (Sandbox Code Playgroud)
现在在你现有的任何控制器中只需编辑第一行或第二行
class <controller_name> extends MY_Controller {
Run Code Online (Sandbox Code Playgroud)
你完成了
另请注意,要在视图中使用的所有变量都在此变量中 (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
}
}
Run Code Online (Sandbox Code Playgroud)