CodeIgniter:load-> helper的范围是什么

Tro*_*ong 0 frameworks codeigniter

在控制器类中,我有这个功能:

public function index(){
    $this->load->helper('url');
    $data['title'] = 'News archive';
    $this->load->view('news/index', $data);
}
Run Code Online (Sandbox Code Playgroud)

我加载了帮助URL,因为我在news/index中使用了anchor().所以看起来在父函数中加载helper就足够了,我不必在ne​​ws/index中加载它.

所以我的问题是CI下面发生了什么让我这样做?是load->查看一个函数,还是粘贴在$ data上执行新闻/索引的结果?load-> view如何识别已在索引中加载的helper url?我仍然试图理解CI框架的工作原理.

还有什么是在我们需要的时候在构造函数或每个函数中加载帮助器的最佳位置?

Lau*_*nce 5

Also what would be the best place to load helper, in the constructor, or in each function as we need it?
Run Code Online (Sandbox Code Playgroud)

作为一个粗略的经验法则;

  • 如果在控制器中使用助手一次 - 将其放在该特定功能中
  • 如果在控制器中的多个位置使用帮助程序 - 将其放在该控制器构造函数中
  • 如果在多个控制器中的多个位置使用帮助程序 - 将其放在"自动加载"部分中一次.