【问题标题】:how to access library in CI HMVC?如何访问 CI HMVC 中的库?
【发布时间】:2017-08-02 00:38:43
【问题描述】:

下面列出了我当前的控制器和库...

>application/
 - config/
 - controllers/
 - ...
 - models/
 - modules/
   - module1/
     - controllers/
       - Test_cont.php
     - models/
     - views/
     - libraries
       - Test_lib.php
 - third_party/
 - views/
 - ...(other files & folders)

'modules/module1/controllers/Test_cont.php'是:

class Test_cont extends MY_Controller
{
  function __construct(){
    parent::__construct();
  }

  function index(){
    $this->load->library('Test_lib');
    $this->Test_lib->doSomething();
  }
}

'modules/module1/libraries/Test_lib.php'文件是:

class Test_lib
{
  function __construct(){
    echo 'library loaded <br>';
  }

  function doSomething(){
    echo 'it works!';
  }
}

当我访问 URL 'http://localhost/codeigniter-3.1.3/module1/test_cont' 它说:

---------------------------------------------------
| An Error Was Encountered                        |
---------------------------------------------------
| Unable to load the requested class: Test        |
---------------------------------------------------

我希望我能让你理解我的问题,如何解决这个问题?... (提前致谢)

【问题讨论】:

  • 尝试像$this-&gt;load-&gt;library('module1/test_lib');这样加载库并使用小写语法。
  • @smokehill 如果你想从另一个模块访问库,你只需要写“模块/库”
  • @Amin 你的问题解决了吗?
  • 是的,它的工作方式如下... $this->load->library('Test_lib');
  • 嗨@Amin - 如果您接受对您有帮助的答案或添加您自己的答案来解决问题,如果缺少正确答案,那就太好了:)

标签: php codeigniter codeigniter-3 hmvc codeigniter-hmvc


【解决方案1】:

如果你在同一个模块中,那么你可以像这样加载库:

function index(){
   $this->load->library('Test_lib');
   $this->test_lib->doSomething();
}

但是如果你在不同的模块中并且你想从不同的模块中加载库,那么:

function index(){
   $this->load->library('module_name/Test_lib');
   $this->test_lib->doSomething();
}

【讨论】:

    【解决方案2】:

    最后我发现脚本内部有一个错误,所以我虽然加载库有任何问题,但我的库已加载:

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

    【讨论】:

      【解决方案3】:

      在hmvc中你需要在加载库、模型等时包含模块名称。

      function index(){
         // You don't need to use upper case when loading library only class and filename
      
         $this->load->library('module-name/test_lib');
         $this->test_lib->doSomething();
      
         // Loading model hmvc
      
         $this->load->model('module-name/test_model');
         $this->test_model->doSomething();
      }
      

      控制器如果没有应用程序/核心/MY_Controller.php 使用 MX_Controller

      文件名 Test_cont.php

      class Test_cont extends MY_Controller
      {
      
      }
      

      如果您需要使用 MY_controller,请确保在 application/core/MY_Controller.php 中执行此操作

      <?php
      
      class MY_Controller extends MX_Controller {
      
      }
      

      【讨论】:

        【解决方案4】:

        库名称不区分大小写。对象实例总是小写。

        creating libraries

        function index(){
           $this->load->library('Test_lib');
           $this->test_lib->doSomething();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-04
          • 2015-11-05
          • 1970-01-01
          • 2019-12-29
          • 2023-03-25
          相关资源
          最近更新 更多