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

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

问题描述

我需要以不同的方式循环许多数组,并将其显示在页面中。数组由模块类生成。我知道它最好不包括函数对'views',我想知道在哪里插入函数文件。

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 helper是一个具有多个函数的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> \application\config\autoload.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>\application\config\autoload.php.

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

- 马修

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

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