2

我正在开发一个 CodeIgniter 项目。我想将一个 uri 段从控制器传递到我自己的库。我无法做到这一点..我使用 session 尝试过,但它不起作用。请帮助我..

这是我的控制器,

<?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();
    }   

}

在库中,我尝试获取会话数据,但我将会话数据 'id' 设为 0 ...当我使用 uri->segemnt(2) 时。它将获得正确的值,但 uri->segment(3) 不起作用...请帮助我

4

2 回答 2

0

在文件夹 application\libraries\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
    }
}
?>

希望它有效!:)

于 2015-02-09T03:22:11.300 回答
0

要在您的库中访问 CodeIgniter 的本机资源,请使用该get_instance()函数。

您必须已将会话库加载到新创建的库中。您应该必须将会话库包含到您自己的库中。您可以在尝试访问会话变量的函数中使用以下代码。

$CI =& get_instance();

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

这篇文章肯定会帮助您利用图书馆中的会话。

于 2012-08-06T07:19:30.127 回答