2012-08-06 91 views
2

我正在研究CodeIgniter項目.. 我想將uri片段從控制器傳遞到我自己的庫。 我不能能夠做到這一點。我嘗試過使用會話,但它不會work.please幫助我..將uri片段傳遞到codeIgniter中的庫

這是我的控制器,

<?php 
class insertdata extends CI_Controller{ 

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

//############################################################################# 

function updateprop(){ 

    $this->load->library('DataEntryForms_update'); 

      //get the uri segment 
    $id = $this->uri->segment(3); 

     //setting the session data 
     $this->session->set_userdata('id',$id); 

    $params [] = $this->uri->segment(3); 


    $form_name = $this->input->post('function_name'); 
    switch ($function_name){ 

     case 1: 

      $this->dataentryforms_update->function1(); 

      break; 
     case 2: 
      $this->dataentryforms_update->function2(); 
      break; 

     }    
    } 
    } 

這是我的圖書館.. 。

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

class DataEntryForms_update{ 
protected $CI; 

####################################################### 
    //gets the CI instance  
    function __construct(){ 

$this->CI =& get_instance(); 
    } 


############################################################# 

    function function1($params){ 

$this->params = $params; 
print_r ($this->params); 

$project_id = $this->CI->session->userdata('id'); 
echo $project_id; 
die(); 
    } 

} 

在圖書館我試圖獲取會話數據,但我得到的會話數據「身份證」爲0 ... 當我使用URI-> segemnt(2)。它會得到正確的值,但是uri-> segment(3)不起作用...請幫助我

+0

echo $ this-> uri-> segment(3);並檢查是否需要值,並檢查控制器本身的回聲會話值。 – Vamsi 2012-08-06 06:28:04

+0

爲什麼你不把id作爲必需的參數傳遞給你的函數? – rrrhys 2012-08-06 06:32:30

回答

0

要訪問庫中的CodeIgniter原生資源,請使用get_instance()函數。

您必須將會話庫加載到新創建的庫中。您必須將會話庫包含到您自己的庫中。您可以將以下代碼用於您試圖訪問會話變量的函數中。

$CI =& get_instance(); 

$CI->load->helper('url'); 
$CI->load->library('session'); 

This post肯定會幫助你利用你的庫中的會話。

+0

謝謝...它適用於我... – 2012-08-06 07:48:29

0

在文件夾應用程序\庫\ MyClass.php

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

class MyClass { 
    public function myfunction() { 
     $CI =& get_instance(); 
     $CI->load->helper('url'); 
     $CI->load->library('session'); 
     // do something else below 
    } 
} 
?> 

希望工程! :)

相關問題