简体   繁体   中英

Function won't pass variable to view

Right now $renderData['username'] won't pass through to the view.

class HomeController extends MY_Controller {

    public function index($renderData=""){  

               if($this->session->userdata('logged_in'))
               {
                 $session_data = $this->session->userdata('logged_in');
                 $renderData['username'] = $session_data['username'];
                 //$this->load->view('pages/home_view', $renderData);
                 $this->_render('pages/home',$renderData);
               }
               else
               {
                 //If no session, redirect to login page
                 redirect('LoginController', 'refresh');
               }               
    }        
}

The error I get is...

在此处输入图片说明


Which alludes to this code...

<h1>Home</h1>
<h2>Welcome <?php echo $username; ?>!</h2>
<a href="home/logout">Logout</a>

Here is my My_Controller where the _render function is located...

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

class MY_Controller extends CI_Controller{

//Page info
protected $data = Array();
protected $pageName = FALSE;
protected $template = "main";
protected $hasNav = TRUE;
//Page contents
protected $javascript = array();
protected $css = array();
protected $fonts = array();
//Page Meta
protected $title = FALSE;
protected $description = FALSE;
protected $keywords = FALSE;
protected $author = FALSE;

function __construct()
{   

    parent::__construct();
    $this->data["uri_segment_1"] = $this->uri->segment(1);
    $this->data["uri_segment_2"] = $this->uri->segment(2);
    $this->title = $this->config->item('site_title');
    $this->description = $this->config->item('site_description');
    $this->keywords = $this->config->item('site_keywords');
    $this->author = $this->config->item('site_author');

    $this->pageName = strToLower(get_class($this));
}


protected function _render($view,$renderData="FULLPAGE") {
    switch ($renderData) {
    case "AJAX"     :
        $this->load->view($view,$this->data);
    break;
    case "JSON"     :
        echo json_encode($this->data);
    break;
    case "FULLPAGE" :
    default         : 
    //static
    $toTpl["javascript"] = $this->javascript;
    $toTpl["css"] = $this->css;
    $toTpl["fonts"] = $this->fonts;

    //meta
    $toTpl["title"] = $this->title;
    $toTpl["description"] = $this->description;
    $toTpl["keywords"] = $this->keywords;
    $toTpl["author"] = $this->author;

    //data
    $toBody["content_body"] = $this->load->view($view,array_merge($this->data,$toTpl),true);

    //nav menu
    if($this->hasNav){
        $this->load->helper("nav");
        $toMenu["pageName"] = $this->pageName;
        $toHeader["nav"] = $this->load->view("template/nav",$toMenu,true);
    }
    $toHeader["basejs"] = $this->load->view("template/basejs",$this->data,true);

    $toBody["header"] = $this->load->view("template/header",$toHeader,true);
    $toBody["footer"] = $this->load->view("template/footer",'',true);

    $toTpl["body"] = $this->load->view("template/".$this->template,$toBody,true);


    //render view
    $this->load->view("template/skeleton",$toTpl);
     break;
}
}
}

Here is an additional function that may be helpful...

class VerifyLogin extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('user','',TRUE);
 }

 function index()
 {
   //This method will have the credentials validation
   $this->load->library('form_validation');

   $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
   $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

   if($this->form_validation->run() == FALSE)
   {
     //Field validation failed.  User redirected to login page
     $this->load->view('pages/login_view');
   }
   else
   {
     //Go to private area
//     redirect('home', 'refresh');
       redirect('HomeController', 'refresh');

   }

 }

 function check_database($password)
 {
   //Field validation succeeded.  Validate against database
   $username = $this->input->post('username');

   //query the database
   $result = $this->user->login($username, $password);

   if($result)
   {
     $sess_array = array();
     foreach($result as $row)
     {
       $sess_array = array(
         'id' => $row->id,
         'username' => $row->username
       );
       $this->session->set_userdata('logged_in', $sess_array);
     }
     return TRUE;
   }
   else
   {
     $this->form_validation->set_message('check_database', 'Invalid username or password');
     return false;
   }
 }
}
?>

What am I doing wrong that is preventing the username to be passed to the view?

PS - let me know any additional documentation I can provide.

something like this

protected function _render($view,data, $renderData="FULLPAGE") {
$this->data = $data;
switch ($renderData) {
case "AJAX"     :
    $this->load->view($view,$this->data);
break;
case "JSON" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM