【问题标题】:Passing uri segement to library in codeIgniter将uri段传递给codeIgniter中的库
【发布时间】:2015-02-09 11:22:11
【问题描述】:

我正在开发一个 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) 不起作用...请帮助我

【问题讨论】:

  • echo $this->uri->segment(3);并检查它是否需要值,并检查控制器本身的回显会话值。
  • 为什么不将 id 作为必需参数传递给函数?

标签: php codeigniter


【解决方案1】:

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

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

$CI =& get_instance();

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

This post 肯定会帮助您利用图书馆中的会话。

【讨论】:

    【解决方案2】:

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

    希望它有效! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      相关资源
      最近更新 更多