Php MVC设计-具有用户组内容的视图
我正在用PHP编写我的第一个MVC应用程序。我不想使用任何框架 现在,我正在寻找一个好的解决方案或最佳实践,以便在视图中为用户显示元素,前提是允许用户与特定元素调用的函数交互 应用程序将在视图中加载模板文件。 在模板文件中,我显示了模型中的数据,这些数据从控制器传递到视图 来自控制器Php MVC设计-具有用户组内容的视图,php,model-view-controller,web-applications,software-design,Php,Model View Controller,Web Applications,Software Design,我正在用PHP编写我的第一个MVC应用程序。我不想使用任何框架 现在,我正在寻找一个好的解决方案或最佳实践,以便在视图中为用户显示元素,前提是允许用户与特定元素调用的函数交互 应用程序将在视图中加载模板文件。 在模板文件中,我显示了模型中的数据,这些数据从控制器传递到视图 来自控制器 public function showUserList() { $userList = $this->model->getUserList(); $this->view->
public function showUserList() {
$userList = $this->model->getUserList();
$this->view->loadTemplate($userList, 'user_list.php');
}
从视图:
class user_view {
public function loadTemplate($data, $file, $buffer= false){
if($buffer === true){
ob_start();
include dirname(__FILE__).'/templates/'.$file;
$c = ob_get_contents();
ob_end_clean();
return $c;
}else{
include dirname(__FILE__).'/templates/'.$file;
}
}
}
模板:
<table class="table" id="data-table">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>E-Mail</th>
<th>Group</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(is_array($data)){
foreach($data as $key => $value){
?>
<tr>
<td><?php echo $data['name'].' '.$data['surname']; ?></td>
<td><?php echo $data['abbr']; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['gr_name']; ?></td>
<td>
<div data-access="," onclick="$user_controller.showUserChangeView('<?php echo $data['id']; ?>')" class="btn btn-primary">edit</div>
<div data-access="checkAccess" onclick="$user_controller.deleteUser('<?php echo $data['id']; ?>')" class="btn btn-danger">delete</div>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
名称
用户名
电子邮件
团体
行动
我的建议是要么实施,要么取决于你觉得有用的东西。我更喜欢RBAC,所以我将给出一个简单的例子
创建用户可以执行的所有可能操作的列表,并定义可能具有访问权限的角色
// List of permissions (the actual term to define resources in RBAC) for the users
$actions = array(
'view' => array(ROLE_USER, ROLE_OWNER, ROLE_MANAGER, ROLE_ADMIN),
'edit' => array(ROLE_OWNER, ROLE_ADMIN),
'delete' => array(ROLE_OWNER, ROLE_ADMIN),
'change_pass' => array(ROLE_OWNER, ROLE_MANAGER, ROLE_ADMIN)
)
现在,当您显示这些操作的按钮时,您可以检查当前用户是否具有允许其访问这些操作的任何角色。如果答案是“是”,那么展示给他们看,否则根本不展示
$currentUserRole = '..'; //Retrieve however you want
foreach($actions as $allowed_roles) {
if(in_array($currentUserRole, $allowed_roels)) {
echo "<div ...>"; // Basically show the action
}
// If not carry on to next action with out showing
}
$currentUserRole='..'//取回你想要的
foreach($actions作为$allowed\u角色){
if(在数组中($currentUserRole,$allowed_roels)){
echo”“;//基本上显示动作
}
//如果未显示,则继续执行下一个操作
}
(注意:仅显示可能不足以确保安全,因此我们还必须在执行操作时进行检查)
更新:您可以将上述代码放在以下部分:
<table class="table" id="data-table">
<thead>
<tr>
<th>Name</th>
<th>Username</th>
<th>E-Mail</th>
<th>Group</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
if(is_array($data)){
foreach($data as $key => $value){
?>
<tr>
<td><?php echo $data['name'].' '.$data['surname']; ?></td>
<td><?php echo $data['abbr']; ?></td>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['gr_name']; ?></td>
<td>
$currentUserRole = '..'; //Retrieve however you want
foreach($actions as $allowed_roles) {
if(in_array($currentUserRole, $allowed_roels)) {
echo "<div ...>"; // Basically show the action
}
// If not carry on to next action with out showing
}
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
名称
用户名
电子邮件
团体
行动
$currentUserRole='..'//取回你想要的
foreach($actions作为$allowed\u角色){
if(在数组中($currentUserRole,$allowed_roels)){
echo”“;//基本上显示动作
}
//如果未显示,则继续执行下一个操作
}
谢谢您的帮助。我应该在哪里实现模板文件中的最后一个代码?@h0mebrewer,我试图在上面for循环的if语句中显示该部分。你具体指的是哪一行代码,因为我可能不确定。我的意思是把这些代码放在哪里:foreach($actions as$allowed_roles){if(in_array($currentUserRole,$allowed_roels)){echo”“;//基本上显示操作}//如果不显示,则继续执行下一个操作}