php - View 中的 CodeIgniter 助手

标签 php codeigniter templates codeigniter-helpers

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

application
--view
---myview.php

在这里我应该调用助手并返回结果

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

echo $myhelper->processor($text);

最佳答案

CodeIgniter 的 user guide说明可以加载助手 并在 View 中使用它们的功能。

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.

然而,在 View 中加载助手并不是最佳实践,因此您可以 auto-load相关的助手,或将其加载到您的 Controller 中。

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.

因此,在 View 中使用辅助函数是可以的,尽管鼓励将辅助函数加载到 Controller 中或自动加载。

关于php - View 中的 CodeIgniter 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16510348/

相关文章:

codeigniter,如何避免由于路由配置不正确而导致重复的内容?

javascript - 为什么 Dreamweaver 显示此脚本行的语法错误?

c++ - X 不是类模板

php - 从数据库中获取数据作为数组

python - 如何授予 www-data 用户访问 python salt 模块的权限?

php - Codeigniter 数据库查询子句不起作用

c++ - C++ 模板中的函数签名

php - 如何判断一个类是内部类还是用户类?

javascript - 我无法从表单提交中调用我的 PHP 代码,这是为什么?

php - 根据条件替换字符串(正则表达式?)