Php 创建合适的控制器
我有一个控制器,它有很多函数,我注意到当我加载我的欢迎视图时,我的get_participants函数没有运行,因为当我尝试使用Php 创建合适的控制器,php,codeigniter,Php,Codeigniter,我有一个控制器,它有很多函数,我注意到当我加载我的欢迎视图时,我的get_participants函数没有运行,因为当我尝试使用时,我得到了未定义的变量异常 我不希望在我的索引函数中包含加载欢迎视图的所有内容,而是包含许多创建一个视图的函数。调用此控制器以使其运行类中的每个函数的正确方法是什么,或者是否有更好的方法 class Welcome extends CI_Controller { function __construct() { parent::__co
时,我得到了未定义的变量异常
我不希望在我的索引函数中包含加载欢迎视图的所有内容,而是包含许多创建一个视图的函数。调用此控制器以使其运行类中的每个函数的正确方法是什么,或者是否有更好的方法
class Welcome extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth_groups','','tank_auth');
$this->load->model('Participant_model');
}
function index()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->load->view('welcome', $data);
}
}
public function get_participants()
{
$data['product'] = $this->Participant_model->get_all_records();
$this->load->view('welcome', $data);
}
}
看法
Hi,
!您现在已登录。
为什么不在索引中调用产品模型呢
function index()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['product'] = $this->Participant_model->get_all_records();
$this->load->view('welcome', $data);
}
}
没有明显的理由将其划分为自己的方法
您还可以设置类变量,并让方法相应地影响它们。这在很大程度上取决于你的需要,尽管一个合适的例子可能并不完全适用
class Welcome extends CI_Controller
{
private $data = array(
'user_id' => null,
'username' => null,
'product' => null
);
function __construct()
{
...
然后让各个方法影响公共$data数组中的某些元素
public function get_participants()
{
$this->data['product'] = $this->Participant_model->get_all_records();
}
并让您的索引方法加载类数组,而不是专门填充它
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth_groups','','tank_auth');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username'] = $this->tank_auth->get_username();
}
$this->load->model('Participant_model');
}
function index()
{
$this->get_participants();
$this->load->view('welcome', $this->data);
}
请注意,这只是一个如何安排课程以满足您需求的示例。不一定是好习惯或任何东西的例子。最终,您应该以合乎逻辑的方式进行编程,以满足您的需求,并且任何正常人都可以合理地阅读。
只是我的意见
我应该说,我认为尝试设置一个类来强制运行其中的每个方法来创建一个视图是一个坏主意。线性执行本质上与将所有内容堆积到索引方法中是一样的。也许我没听清你的话。你为什么不把产品模型称为索引呢
function index()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$data['product'] = $this->Participant_model->get_all_records();
$this->load->view('welcome', $data);
}
}
没有明显的理由将其划分为自己的方法
您还可以设置类变量,并让方法相应地影响它们。这在很大程度上取决于你的需要,尽管一个合适的例子可能并不完全适用
class Welcome extends CI_Controller
{
private $data = array(
'user_id' => null,
'username' => null,
'product' => null
);
function __construct()
{
...
然后让各个方法影响公共$data数组中的某些元素
public function get_participants()
{
$this->data['product'] = $this->Participant_model->get_all_records();
}
并让您的索引方法加载类数组,而不是专门填充它
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth_groups','','tank_auth');
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$this->data['user_id'] = $this->tank_auth->get_user_id();
$this->data['username'] = $this->tank_auth->get_username();
}
$this->load->model('Participant_model');
}
function index()
{
$this->get_participants();
$this->load->view('welcome', $this->data);
}
请注意,这只是一个如何安排课程以满足您需求的示例。不一定是好习惯或任何东西的例子。最终,您应该以合乎逻辑的方式进行编程,以满足您的需求,并且任何正常人都可以合理地阅读。
只是我的意见
我应该说,我认为尝试设置一个类来强制运行其中的每个方法来创建一个视图是一个坏主意。线性执行本质上与将所有内容堆积到索引方法中是一样的。也许我没领会你的意思