【问题标题】:Is there a difference between the method of helper and library are called upon in code igniter?在代码点火器中调用帮助程序和库的方法有区别吗?
【发布时间】:2014-03-07 12:25:47
【问题描述】:

我有点困惑,代码点火器中使用库和帮助器的方法的方式。我还在学习代码点火器。

控制器

function index(){
    $this->load->helper('text');
    $this->load->library('auth'); //custom library

    $data['string'] = 'this is sample ..... this is sample';
    $this->load->view('article', $data);
}

查看

<?php 
if(is_logged_in()){    //is_logged_in() is the method from the library, 'auth'
    echo 'You are logged in';
}
<p><?php echo word_limiter($string, 10); ?></p> <!--word_limiter() is the method from the helper, 'text' -->

在上面的视图文件中,辅助方法word_limiter() 工作正常。但是方法is_logged_in() 不起作用。但如果我这样做($this-&gt;auth-&gt;is_logged_in()),它会起作用。

但是为什么来自助手的方法,即word_limiter() 不必这样写($this-&gt;text-&gt;word_limiter())。

调用助手和库的方法有区别吗?

【问题讨论】:

    标签: php codeigniter codeigniter-2 codeigniter-helpers


    【解决方案1】:

    CodeIgniter 助手是一组相关的函数(通用函数),您可以在 ModelsViewsControllers 中使用它们。 . 无处不在。

    一旦您加载(包含)该文件,您就可以访问这些功能。

    但是库是一个类,你需要创建一个类的实例($this-&gt;load-&gt;library())。您需要使用对象$this-&gt;... 来调用这些方法。

    作为一个经验法则:库用于面向对象的上下文(控制器,...),而帮助器更适合在 视图中使用 (非面向对象)。

    【讨论】:

      【解决方案2】:

      CI Helper 可能有也可能没有类

      但是库必须有类表示。

      参考这个 SO 答案

      CodeIgniter: Decision making for creating of library & helper in CodeIgniter

      【讨论】:

      • 是的,该链接为我提供了更多说明。
      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2012-03-03
      • 2013-02-03
      • 2013-03-22
      • 1970-01-01
      • 2017-01-02
      • 2023-03-29
      相关资源
      最近更新 更多