如何在codeigniter上获得权限的菜单列表?

时间:2016-08-11 20:10:27

标签: php codeigniter

我想显示一个菜单列表,其中包含具有权限的respetives模块。我有我的模型和控制器,但我不知道如何将它传递到我的主视图并显示它。要么直接用于php,要么是ajax。

控制器

 <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends MY_Controller {
    public function __construct(){
        parent::__construct();
    }

    public function index(){
        $data['module'] = 'Login';
        $this->load->view('login',$data);
    }

    public function getAccess(){
        if ($this->session->userdata('logged_in') == TRUE){
            redirect('home');
        }else{
            $username = $this->security->xss_clean($this->input->post('username'));
            $password = $this->security->xss_clean($this->input->post('password'));
            $array = $this->user->login($username,$password);
            if ($array[0] == 0) {
                echo 0;
            }else{
                $data_session = array(
                    'id' => $array[0]['id'],
                    'name' => $array[0]['nombre'],
                    'last_name' => $array[0]['apellido'],
                    'type' => $array[0]['id_perfil'],
                    'logged_in' => TRUE 
                );
                $this->session->set_userdata('log',$data_session);
            }
        }
    } 

    public function logout(){
        $this->session->sess_destroy();
        redirect('login');
    }

    public function getModules($module_id){
        if ($this->session->userdata('log')){
            $data = $this->session->userdata('log');
            $menu = array();
            $seccions = $this->module->get_rows();
            foreach ($seccions as $index => $seccion) {
                //echo json_encode($seccion);
                $modules = $this->module->query("SELECT concat('".$seccion['id']."',storelte_modulo.id) AS id, storelte_modulo.modulo AS VALUE,storelte_modulo.seccion_id,concat('".base_url()."',storelte_modulo.url) as url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON  storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id =  $data[id] AND storelte_modulo_perfil.status = 1");
                $seccions[$index]['data']= $modules;
                if(!count($seccions[$index]['data']))
                    unset($seccions[$index]);
            }
            foreach($seccions as $item)
                array_push($menu,$item);
            $this->json($menu);
        }
    }
}

module_model

class Module extends CI_Model {
    public function __construct(){
        parent::__construct();

    }

    public function get_rows(){
        $this->db->select('id,seccion');
        $this->db->from('storelte_seccion');
        return $this->db->get()->result_array();
    }

    public function query($query){
        return $this->db->query($query)->result_array();
    }

}

html将显示具有权限的模块

<div class="row">
              <h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
              <div class="home_module_list">

              </div>
          </div>

1 个答案:

答案 0 :(得分:1)

为了通过php编写模块列表,你必须将$ menu数组传递给视图文件(html)。如果您这样做,function getModules()应该:

  • 返回$ menu数组,如果从Controler的另一个方法调用getModules()

- 或 -

  • 将数组直接传递给视图文件(代码示例的最简单方法)。因此,在这种情况下,请将Controler上的函数getModules()更改为:
    public function getModules($module_id){
        if ($this->session->userdata('log')){
          $data = $this->session->userdata('log');
          $menu = array();
          $seccions = $this->module->get_rows();
          foreach ($seccions as $index => $seccion) {
                $modules = $this->module->query("SELECT concat('". $seccion['id'] ."',storelte_modulo.id) AS id, storelte_modulo.modulo AS VALUE, storelte_modulo.seccion_id, concat('". base_url() ."',storelte_modulo.url) as url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id =  $data[id] AND storelte_modulo_perfil.status = 1");
                 // assuming you get 1 row only from the above query
                 if (!empty($modules;)) {
                    $modules['index'] = $index;
                    $menu['seccions'] = $modules;
                 }
           }
           $this->load->view('modules_view',$menu);
         }
    }

然后在您的视图文件(modules_view.php)中,您可以遍历$菜单项:

<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
  <ul>
  <?php
     foreach($seccions as $session) {
        echo '<li><a href="'. $session['url'] .'">'. $session['index'] .' - '. $session['id'] .' - '. $session['VALUE'] .'</a></li>';
     }
  ?>
  </ul>
</div>
</div>

...

如果你想通过ajax传递菜单,你必须在你的html文件中使用javascript,但首先你的函数getModules()应该是echo JSON数组(或者你要嵌入的html)在“home_module_list”div中。在这种情况下,我建议你给div id