CodeIgniter:创建新的帮助程序? [英] CodeIgniter: Create new helper?

查看:27
本文介绍了CodeIgniter:创建新的帮助程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以不同的方式循环大量数组并将其显示在页面中.数组由模块类生成.我知道最好不要在视图"中包含函数,我想知道在哪里插入函数文件.

I need to loop lot of arrays in different ways and display it in a page. The arrays are generated by a module class. I know that its better not to include functions on 'views' and I want to know where to insert the functions file.

我知道我可以扩展"助手,但我不想扩展助手.我想用我的循环函数创建一个助手..让我们称之为 loops_helper.php

I know I can 'extend' the helpers, but I don't want to extend a helper. I want to kind of create a helper with my loop functions.. Lets call it loops_helper.php

推荐答案

CodeIgniter 助手是一个具有多种功能的 PHP 文件.这不是一个类

A CodeIgniter helper is a PHP file with multiple functions. It is not a class

创建一个文件并将以下代码放入其中.

Create a file and put the following code into it.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('test_method'))
{
    function test_method($var = '')
    {
        return $var;
    }   
}

将其保存到 application/helpers/ .我们将其称为new_helper.php"

Save this to application/helpers/ . We shall call it "new_helper.php"

存在第一行是为了确保文件不能被包含和从 CodeIgniter 范围之外运行.这之后的一切都是不言自明的.

The first line exists to make sure the file cannot be included and ran from outside the CodeIgniter scope. Everything after this is self explanatory.

这可以在您的控制器模型视图(不可取)

This can be in your controller, model or view (not preferable)

$this->load->helper('new_helper');

echo test_method('Hello World');

如果你在很多地方使用这个助手,你可以通过将它添加到自动加载配置文件来自动加载它,即 <your-web-app>applicationconfigautoload.php.

If you use this helper in a lot of locations you can have it load automatically by adding it to the autoload configuration file i.e. <your-web-app>applicationconfigautoload.php.

$autoload['helper'] = array('new_helper');

-马修

这篇关于CodeIgniter:创建新的帮助程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆