我不知道上面的标题是否是关于我的问题的正确标题。
有没有办法声明一个变量,我们可以从视图中的任何地方访问它,而无需在控制器的每个函数中再次重新定义它?
例如在控制器文件中,Students.php包含许多处理视图的函数,请看下面的内容:
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
我希望我们可以访问$count_student
和view-students.php
中的view-detail-student.php
,而无需在处理视图的每个函数上定义$data['count_student'] = $this->m_data->allStudent();
。
有可能这样做吗?
答案 0 :(得分:1)
在application / core / create MY_Controller.php
中<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
控制器使用$this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
现在您已经扩展了控制器,您可以像
一样<?php echo $count_student;?>
在视图
上答案 1 :(得分:0)
如果在每个控制器功能中使用相同的变量,则必须按如下方式声明
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
仅当此控制器中的所有视图都有公共变量时才适用。 如果您使用的是不同的变量,请使用以下代码。
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}
答案 2 :(得分:0)
我注意到,如果在控制器中使用$this->
声明变量,则可以在视图中访问变量而不传递变量。可能是因为他们默认公开可见度。
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
然后在视图中,您可以使用$this->count_all_the_students
而不将其放在$ data数组中。
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';