动态菜单/菜单创建控制器?

时间:2018-10-23 10:17:35

标签: codeigniter codeigniter-3

我正在用CI构建一个小型应用程序。我正在使用带有单独菜单文件的预制模板。该菜单文件包含在综合浏览量中:

<?php include('include/sidebar.php'); ?>

现在,我想根据用户权限使菜单中的项目动态化。在sidebar.php中,我定义了如下菜单项:

<?php

$classname = "logs";
if (check_class($classname) == true){
    ?>
    <li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname){echo "active";}?>">
        <a href="javascript:void(0);" class="menu-toggle">
            <i class="material-icons">youtube_searched_for</i>
            <span><?php echo $this->lang->line('menu_logs') ?></span>
        </a>
        <ul class="ml-menu">

            <?php
            $methodname = "viewlogs";
            if (check_method($classname,$methodname) == true){
                ?>
                <li id="<? echo $classname;?>" class="<?php if($this->uri->segment(1)==$classname AND $this->uri->segment(2)==$methodname){echo "active";}?>">
                    <a href="<?= base_url($classname."/".$methodname); ?>"><?php echo $this->lang->line('menu_logs') ?></a>
                </li>
            <?php }?>

        </ul>
    </li>

<?php }?>

check_class和check_method当前也包含在sidebar.php文件中:

<?php

// This should not be here...

global $thisglobal;
$thisglobal = $this;

global $auth_roleglobal;
$auth_roleglobal = $auth_role;

function check_class($class) {
    global $thisglobal;

    //Override if admin
    if ($thisglobal->auth_role == "admin") {
        return true;
    }

    // Get current roles permissions
    $role_arr_flipped     = array_flip(array($thisglobal->auth_role)); // Avoid Error @ Only variables should be passed by reference
    $role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
    $role_perms           = array_shift($role_arr_intersected);

    if (array_key_exists($class, $role_perms)) {
        return true;
    } else {
        return false;
    }
}

function check_method($class,$method) {
    global $thisglobal;

    //Override if admin
    if ($thisglobal->auth_role == "admin") {
        return true;
    }

    // Get current roles permissions
    $role_arr_flipped     = array_flip(array($thisglobal->auth_role)); // Avoid Error @ Only variables should be passed by reference
    $role_arr_intersected = array_intersect_key($thisglobal->config->item('user_role_permissions'), $role_arr_flipped);
    $role_perms           = array_shift($role_arr_intersected);

    // Flip arrary
    $role_perms["$class"] = array_flip($role_perms["$class"]);

    if (array_key_exists($method, $role_perms["$class"])) {
        return true;
    } else {
        return false;
    }
}

?>

这可行,但是显然将那些函数包含在视图文件中是违反MVC方法的,我可能还想在其他视图中重用check_class和check_method。我已经将这些功能移到了my_controller上,但是同样,我不应该从我的观点调用这些功能。

我有点迷失于继续...

边栏没有自己的控制器。我应该创建一个单独的吗?但是然后如何加载它,因为我不能(不应该)从页面视图中调用菜单控制器。

或者我应该在加载视图之前调用check_class和check_method,但是我还不知道那时应该检查哪些菜单项。

谢谢!

1 个答案:

答案 0 :(得分:1)

我将在其中创建一个名为Menu.php的库,并创建用于检查用户权限和内容的函数,并具有仅输出菜单的render方法。

这样,您的控制器将加载该库。向其中发送一些数据,并以字符串形式获取菜单。然后,您只需将该字符串发送到视图并回显它即可。

其他选项可能会调查演示者模式,并尝试在codeigniter中实现该模式。

Presenter pattern

Presenter library for ci

相关问题