gpt4 book ai didi

php - 从 CodeIgniter 中的核心 Controller 获取数据

转载 作者:行者123 更新时间:2023-11-29 10:29:22 25 4
gpt4 key购买 nike

我正在开发一款社交网络应用。我遇到一个问题,我试图从数据库中名为 users 的表中获取名称和用户名。为了能够在不单独访问所有 Controller 的情况下执行此操作,我必须创建一个名为 MY_Controller 的核心 Controller 。

问题如下(MY_Controller 没有公共(public) $users):

<?php

class MY_Controller extends CI_Controller
{

//public $users = array('username' => $this->input->post('username'));


function __construct()
{
parent::__construct();

//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$this->users = $this->User_model->get_list();

//Load Menu Library
$this->load->library('menu');
$this->pages = $this->menu->get_pages();

// Brand/Logo
$this->favicon = 'https://blogpersonal.net/wp-content/uploads/2017/08/favicon-v2-150x150.png';
$this->brand = 'Some Name';
$this->description = 'Some Description';
$this->credits = 'https://blogpersonal.net/';
$this->version = '1.1.1';
}
}

class Admin_Controller extends MY_Controller
{

function __construct()
{
parent::__construct();

if (!$this->session->is_admin) {
redirect('admin/login');
}

//some code here

}
}

class Public_Controller extends MY_Controller{
function __construct(){
parent::__construct();

//some code here

}

}

class Social_Controller extends MY_Controller
{

function __construct()
{
parent::__construct();

if (!$this->session->is_member) {
redirect('dashboard/login');
}

//some code here

}
}

如您所见,我使用 __construct 函数加载了 User_model,使其可用并在所有 Controller 中使用它。

我想在名为 templates 的文件夹中获取数据。 View >模板>任何文档但是当我尝试在这样的 View 中获取它时:

    <?php if($this->users) : ?>
<li class="dropdown user user-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar-->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="user-image" alt="User Image">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span class="hidden-xs"><?php echo $this->users['username']; ?></span>
</a>

</li>
<?php endif; ?>

这是旧错误。它向我显示一个错误。难道我做错了什么?有人可以解释一下如何解决它吗?这是我第一次无法获取数据。这是我得到的结果:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: users

Filename: Templates/public.php

Line Number: 161

Backtrace:

File: C:\xampp\htdocs\codeigniter\application\views\Templates\public.php Line: 161 Function: _error_handler

File: C:\xampp\htdocs\codeigniter\application\libraries\Template.php Line: 34 Function: view

File: C:\xampp\htdocs\codeigniter\application\controllers\Pages.php Line: 12 Function: load

File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function: require_once

这是我的模型(User_model),以防您可能需要它:

<?php

class User_model extends CI_MODEL
{

function __construct()
{
parent::__construct();
$this->table = 'users';
}

public function get_list()
{
$query = $this->db->get($this->table);
return $query->result();
}

public function get($id)
{
$this->db->where('id', $id);
$query = $this->db->get($this->table);
return $query->row();
}

public function add($data)
{
$this->db->insert($this->table, $data);
}

public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update($this->table, $data);
}

public function delete($id)
{
$this->db->where('id', $id);
$this->db->delete($this->table);
}

public function login($username, $password)
{
$this->db->select('*');
$this->db->from($this->table);
$this->db->where('username', $username);
$this->db->where('password', $password);
$this->db->limit(1);

$query = $this->db->get();

if ($query->num_rows() == 1) {
return $query->row()->id;
} else {
return false;
}
}
}

现在,根据向我建议的更改,它现在显示一个新错误:

新错误:

A PHP Error was encountered

Severity: Notice Message: Undefined index: username Filename: Templates/public.php Line Number: 168

Backtrace:

File: C:\xampp\htdocs\codeigniter\application\views\Templates\public.php Line: 168 Function: _error_handler

File: C:\xampp\htdocs\codeigniter\application\libraries\Template.php Line: 34 Function: view

File: C:\xampp\htdocs\codeigniter\application\controllers\public\Dashboard.php Line: 13 Function: load

File: C:\xampp\htdocs\codeigniter\index.php Line: 315 Function: require_once

现在,如果取消注释核心 Controller (MY_Controller)中的公共(public) $users ,它也会显示一个新错误:

公共(public) $users 在 __construct 函数之外出现新错误

Fatal error: Constant expression contains invalid operations in C:\xampp\htdocs\codeigniter\application\core\MY_Controller.php on line 6 A PHP Error was encountered

Severity: Compile Error

Message: Constant expression contains invalid operations

Filename: core/MY_Controller.php

Line Number: 6

Backtrace:

这是我尝试将数据获取到的图片:

enter image description here

我做错了什么吗?感谢您的帮助。

这是 View (public.php)文件:

<ul class="nav navbar-nav">
<?php if(!$this->session->is_member) : ?>
<li><?php echo anchor('public/users/login', 'Login'); ?></li>
<li><?php echo anchor('public/users/register', 'Register'); ?></li>
<?php else : ?>
<!-- Messages: style can be found in dropdown.less-->
<li class="dropdown messages-menu">
<!-- Menu toggle button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-envelope-o"></i>
<span class="label label-success">4</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 4 messages</li>
<li>
<!-- inner menu: contains the messages -->
<ul class="menu">
<li><!-- start message -->
<a href="#">
<div class="pull-left">
<!-- User Image -->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="img-circle" alt="User Image">
</div>
<!-- Message title and timestamp -->
<h4>
Support Team
<small><i class="fa fa-clock-o"></i> 5 mins</small>
</h4>
<!-- The message -->
<p>Why not buy a new awesome theme?</p>
</a>
</li>
<!-- end message -->
</ul>
<!-- /.menu -->
</li>
<li class="footer"><a href="#">See All Messages</a></li>
</ul>
</li>
<!-- /.messages-menu -->
<!-- Notifications Menu -->
<li class="dropdown notifications-menu">
<!-- Menu toggle button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>
<span class="label label-warning">10</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 10 notifications</li>
<li>
<!-- Inner Menu: contains the notifications -->
<ul class="menu">
<li><!-- start notification -->
<a href="#">
<i class="fa fa-users text-aqua"></i> 5 new members joined today
</a>
</li>
<!-- end notification -->
</ul>
</li>
<li class="footer"><a href="#">View all</a></li>
</ul>
</li>
<!-- Tasks Menu -->
<li class="dropdown tasks-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-flag-o"></i>
<span class="label label-danger">9</span>
</a>
<ul class="dropdown-menu">
<li class="header">You have 9 tasks</li>
<li>
<!-- Inner menu: contains the tasks -->
<ul class="menu">
<li><!-- Task item -->
<a href="#">
<!-- Task title and progress text -->
<h3>
Design some buttons
<small class="pull-right">20%</small>
</h3>
<!-- The progress bar -->
<div class="progress xs">
<!-- Change the css width attribute to simulate progress -->
<div class="progress-bar progress-bar-aqua" style="width: 20%" role="progressbar" aria-valuenow="20" aria-valuemin="0" aria-valuemax="100">
<span class="sr-only">20% Complete</span>
</div>
</div>
</a>
</li>
<!-- end task item -->
</ul>
</li>
<li class="footer">
<a href="#">View all tasks</a>
</li>
</ul>
</li>
<!-- User Account Menu -->
<?php if($this->users) : ?>
<li class="dropdown user user-menu">
<!-- Menu Toggle Button -->
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<!-- The user image in the navbar-->
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="user-image" alt="User Image">
<!-- hidden-xs hides the username on small devices so only the image appears. -->
<span class="hidden-xs"><?php echo $this->users['username']; ?></span>
</a>
<ul class="dropdown-menu">
<!-- The user image in the menu -->
<li class="user-header">
<img src="<?php echo base_url(); ?>assets/img/user2-160x160.jpg" class="img-circle" alt="User Image">
<p><?php echo $this->users['name']; ?> - Web Developer<small>Member since Nov. 2012</small></p>
</li>
<!-- Menu Body -->
<li class="user-body">
<div class="row">
<div class="col-xs-4 text-center">
<a href="#">Followers</a>
</div>
<div class="col-xs-4 text-center">
<a href="#">Sales</a>
</div>
<div class="col-xs-4 text-center">
<a href="#">Friends</a>
</div>
</div>
<!-- /.row -->
</li>
<!-- Menu Footer-->
<li class="user-footer">
<div class="pull-left">
<a href="#" class="btn btn-default btn-flat">Profile</a>
</div>
<div class="pull-right">
<a href="<?php echo base_url(); ?>public/users/logout" class="btn btn-default btn-flat">Sign out</a>
</div>
</li>
</ul>
</li>
<?php endif; ?>
<li>
<a href="#" data-toggle="control-sidebar"><i class="fa fa-gears"></i></a>
</li>
<?php endif; ?>
</ul>

最佳答案

您目前拥有此...而我仅在此处显示片段...

class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct();

//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
$data['users'] = $this->User_model->get_list();

<<<<snip>>>>

建议您将本地定义的 $data['users'] 转换为“属性”

所以我们现在有两个改变......

1 声明属性

2 设置属性。

class MY_Controller extends CI_Controller
{
public $users = array(); // Add the property $user, safe def of an empty array

function __construct()
{
parent::__construct();

//Get user data to make it available for both Admin and Public Controllers
$this->load->model('User_model');
//$data['users'] = $this->User_model->get_list();
// Give the property something to share.
$this->users = $this->User_model->get_list();


<<<<snip>>>>

现在,所有扩展 MY_Controller 的 Controller 现在都可以访问在 MY_Controller 中创建的 $this->users...

因此,您可以在要使用它的地方引用 $this->users

我建议您首先阅读类、属性、方法和继承。通常这足以让你变得危险。这需要一点时间来适应,但值得付出努力......

更新:在调用 View 的 Controller 中,您会执行以下操作$data['users'] = $this->users;

在您看来,您现在像以前一样将其称为 $users->field_name 。我们所做的就是在您的基类中定义 $users,现在可以通过 $this->users 访问它,以便扩展 MY_Controller 类的所有 Controller /方法都可以访问它。

关于php - 从 CodeIgniter 中的核心 Controller 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47706239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com