Codeigniter - 将数据从MY_Controller传递到Main_controller以进行查看

时间:2015-07-22 09:50:15

标签: codeigniter class controllers

这是完整的代码(我想通过MY_Controller上的变量集选择视图): 我想传递那个变量,但它并没有达到"它给我的观点 $ ses_group =" not_logged_in"测试回音,我没有在MY_Controller上设置任何东西

class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
    parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
    $this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
    $this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
    $this->special_data['ses_group'] = 'admin';
}else{
    //  $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}

Main_Controller:

function index(
$data = array(
                    'ses_group' => $this->special_data
            );
            $this->load->view('auth/descricao_anuncio', $data);
)

观点:

<?php if($ses_group="not_logged_in"){ ?>
<li><a href="<?php echo base_url("login/login")?>" class="">Login</a></li>


<?php  }elseif($ses_group="users"){ ?>

<li><a href="<?php echo base_url("users_controller/edit_user")?>">Your Area</a></li>
<li><a href="<?php echo base_url("login/logout")?>">Logout</a></li>


<?php  }elseif($ses_group="empresas"){  ?>

<li><a href="<?php echo base_url("empresas_controller/edit_empresa")?>">Empresa</a></li>
<li><a href="<?php echo base_url("login/logout")?>">Logout</a></li>

<?php  }?>

echo $ses_group;

非常感谢!

2 个答案:

答案 0 :(得分:1)

控制器

 <?php

class MY_Controller extends CI_Controller {

protected $special_data = array();

 function MY_Controller ()  {
        parent::Controller();
    }

function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}

}

您可以使用$this->

中的MY_Controller将您的值从控制器发送到观看次数

<强>控制器

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


      $this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
      $this->load->view('view_x', $this->data);

}

<强>视图

<?php echo $group; ?>

答案 1 :(得分:-2)

这是你需要的吗

控制器

    class MY_Controller extends CI_Controller {

    protected $special_data = array();

function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}

class Main_controller extends MY_Controller {


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

    }


public function index()
    {

    $data = array(

                    'group' => $this->special_data('a')
            );

            $this->load->view('view_x', $data); 
   }

}

视图

<?php echo $group; ?>