簡體   English   中英

在CodeIgniter中加載多個庫 - PHP

[英]Loading more than one libraries in CodeIgniter - PHP

我需要在CI控制器中加載2個以上的庫並調用它們的成員函數。 我嘗試了以下方法,但沒有用。

1。

    $this->load->library('liba');
    $result = $this->liba->SearchTours($searchParams);
    echo $result;

    $this->load->library('libb');
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

2。

    $this->load->library(array('liba' , 'libb'));

    $result = $this->liba->SearchTours($searchParams);
    echo $result;
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

3。

    $this->load->library('liba');
    $this->load->library('libb');

    $result = $this->liba->SearchTours($searchParams);
    echo $result;
    $result2 = $this->libb->tourFetch($searchParams);
    echo $result2;

4。

    public function __construct(){
      parent::__construct();
      $this->load->library('liba');
      $this->load->library('libb');
    }
  1. 我在4個案例中交換了加載順序。 無論哪個提到第二個都不加載。

在所有情況下,只加載第一個提到的庫,本通知顯示第二個。

A PHP Error was encountered Severity: Notice Message: Undefined property: Unify::$libb Filename: controllers/Unify.php Line Number: 30

我在CI用戶手冊或SO上找不到任何靈魂。 我究竟做錯了什么?

在CI中加載多個庫只需傳入它的數組

$this->load->library( array('liba', 'libb') );

和這個通知,因為你沒有包括

$CI =& get_instance(); 

在你的控制器內

該文件是您的controllers文件夾中的文件。 如果是,那么您可能沒有正確擴展您的控制器。 喜歡:

myController extends CI_Controller{}

否則你需要使用$CI =& get_instance(); 在你的文件中,然后使用

$CI->load->library('your_library');

編輯: 注意:檢查您的庫文件是否大寫inoyur文件夾結構,如Libb.php

無論何時使用庫和幫助程序並且需要CI中的內置方法,請使用

$CI =& get_instance();

然后像這樣使用它

$CI->load->helper('url');
$CI->load->library('session'); 
$CI->config->item('base_url');  //...etc

有關詳細信息,請參閱codeigniter.com/userguide2/general/creating_libraries.html


或者為了更好的解決方案,您可以使用codeigniter + HMVC從另一個控制器調用控制器,避免任何沖突並簡化您的故障排除

MVC vs HMVC用於Web應用程序開發

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM