您好,发现了基于 codeigniter 的免费源代码,
我在 codeigniter 中有一个 Controller ,一些代码是这样的:
class front extends main
{
function __construct()
{
parent::__construct();
$this->load_main_vars();
}
private function load_main_vars()
{
$vars['title'] = $this->option->get('title');
$vars['keywords'] = $this->option->get('keywords');
$vars['description'] = $this->option->get('description');
$this->db->order_by('category_name', 'ASC');
$this->db->where('category_active', 1);
$vars['categories'] = $this->db->get_where('categories', array('category_state' => 1))->result();
$this->load->vars($vars);
}
}
然后我有另一个扩展前端的 Controller (上面的代码):
class Site extends front
{
function __construct()
{
parent::__construct();
}
function index()
{
$vars['site'] = $this;
$this->view('site/index', $vars);
}
}
我的问题是为什么我必须在站点 Controller 中使用 this $vars['site'] = $this;
来使用在前端 Controller 中添加的 vars 数组并传递给 View ,
谢谢
最佳答案
我认为你的代码根本无法在 CI 中运行,我只会回答你的问题
一个 Controller 不能扩展另一个 Controller ,CI不允许这样做,而是你需要在你的application/core目录中创建一个My_controller.php,在那里添加必要的代码并在你的 Controller 中扩展该类,现在开始我我将根据您的代码为您提供一个示例
application/core/My_controller.php
class Main extends CI_controller {
// methods and vars....
}
class Front extends Main {
function __construct() {
parent::__construct();
$this->load_main_vars();
}
function load_main_vars() {
$vars = [];
//assigning some vars to $vars
$this->load->vars($vars);
}
}
现在您可以创建 Controller 了:
application/controllers/Site.php
class Site extends Front {
function __construct() {
parent::__construct();
}
function index() {
$this->view('site/index'); // now all vars are accesable in your view
}
}
这是在 CI 中实现您想要的目标的正确方法
关于php - Controller 之间的 codeigniter $this->load->vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447134/