php 视图中的 CodeIgniter 助手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16510348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:15:20  来源:igfitidea点击:

CodeIgniter helper in the view

phpcodeignitertemplatescodeigniter-helpers

提问by fefe

Is it a good workaround and would it be possible to use helper classes in the view, in CodeIgniter. I have a situation when I have to extract with regulars expression from a text couple of strings and generate outputs on matches. I would not like to do this directly in the view and I would like to use for this purpose a helper.

这是一个很好的解决方法,是否可以在 CodeIgniter 中的视图中使用帮助程序类。我有一种情况,当我必须从一对文本字符串中提取正则表达式并在匹配项上生成输出时。我不想直接在视图中执行此操作,我想为此目的使用助手。

application
--view
---myview.php

and here I should call the helper and return results

在这里我应该打电话给助手并返回结果

for example I want to extract from the text the type of processor, than I pass the text and get returned the processor type. This one is needed because all the data in the view are generated by an API dynamically.

例如,我想从文本中提取处理器的类型,然后传递文本并返回处理器类型。之所以需要这个,是因为视图中的所有数据都是由 API 动态生成的。

echo $myhelper->processor($text);

采纳答案by Thomas Decaux

get_instance()->load->helper('HELPER_NAME');

回答by jleft

CodeIgniter's user guideexplains that helperscan be loaded and their function used in views.

CodeIgniter 的用户指南解释了可以加载帮助程序及其在视图中使用的功能。

CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.

CodeIgniter 默认不加载 Helper 文件,所以使用 Helper 的第一步是加载它。加载后,它将在您的控制器和视图中全局可用。

However it is not best pratice to load a helper in a view, so you could either auto-loadthe relevant helper, or load it in your controller(s).

然而,在视图中加载帮助程序并不是最佳实践,因此您可以自动加载相关帮助程序,或者将其加载到您的控制器中。

A helper can be loaded anywhere within your controller functions (or even within your View files, although that's not a good practice), as long as you load it before you use it. You can load your helpers in your controller constructor so that they become available automatically in any function, or you can load a helper in a specific function that needs it.

可以在您的控制器函数中的任何位置(甚至在您的视图文件中,尽管这不是一个好习惯)加载助手,只要您在使用它之前加载它。您可以在控制器构造函数中加载您的助手,以便它们在任何函数中自动可用,或者您可以在需要它的特定函数中加载助手。

So, using helper functions in a view is fine, although it is encouraged that the helper is loaded in a controller, or auto-loaded.

因此,在视图中使用辅助函数是可以的,尽管鼓励将辅助函数加载到控制器中或自动加载。

回答by ganji

Just load the helper in your controller, then

只需在您的控制器中加载助手,然后

$this->load->helper('MY_common_functions');
$template['content'] = $this->load->view('your_view');

In the view call your function name directly. In this case I called my convertor function

在视图中直接调用您的函数名称。在这种情况下,我调用了我的转换器函数

echo convertor($params);