php CodeIgniter:创建新的帮助程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/804399/
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-24 23:54:30  来源:igfitidea点击:

CodeIgniter: Create new helper?

phpcodeignitercodeigniter-helpers

提问by Jonathan

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.

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

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

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

回答by The Pixel Developer

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

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

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;
    }   
}

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

将其保存到application/helpers/。我们将其称为“new_helper.php”

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.

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

Using the Helper

使用助手



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

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

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

echo test_method('Hello World');

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.

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

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

-Mathew

-马修

回答by r4ccoon

Some code that allows you to use CI instance inside the helper:

一些允许您在 helper 中使用 CI 实例的代码:

function yourHelperFunction(){
    $ci=& get_instance();
    $ci->load->database(); 

    $sql = "select * from table"; 
    $query = $ci->db->query($sql);
    $row = $query->result();
}

回答by Eduardo Chavira

Well for me only works adding the text "_helper"after in the php file like:

对我来说,只能"_helper"在 php 文件中添加文本,例如:

Codeiginiter Helpers

Codeiginiter 助手

And to load automatically the helper in the folder aplication -> file autoload.phpadd in the array helper's the name without "_helper" like:

并自动加载文件夹aplication -> 文件 autoload.php中的助手,在数组助手的名称中添加不带“_helper”的名称,例如:

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

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

And with that I can use all the helper's functions

有了它,我可以使用所有助手的功能

回答by lima

To create a new helper you can follow the instructions from The Pixel Developer, but my advice is not to create a helper just for the logic required by a particular part of a particular application. Instead, use that logic in the controller to set the arrays to their final intended values. Once you got that, you pass them to the view using the Template Parser Classand (hopefully) you can keep the view clean from anything that looks like PHP using simple variables or variable tag pairs instead of echos and foreachs. i.e:

要创建新的助手,您可以按照The Pixel Developer说明进行操作,但我的建议是不要仅为特定应用程序的特定部分所需的逻辑创建助手。相反,在控制器中使用该逻辑将数组设置为其最终预期值。一旦你得到了它,你就可以使用模板解析器类将它们传递给视图,并且(希望)你可以使用简单的变量或变量标签对而不是回声和 foreachs 来保持视图与任何看起来像 PHP 的东西无关。IE:

{blog_entries}
<h5>{title}</h5>
<p>{body}</p>
{/blog_entries}

instead of

代替

<?php foreach ($blog_entries as $blog_entry): ?>
<h5><?php echo $blog_entry['title']; ?></h5>
<p><?php echo $blog_entry['body']; ?></p>
<?php endforeach; ?>

Another benefit from this approach is that you don't have to worry about adding the CI instance as you would if you use custom helpers to do all the work.

这种方法的另一个好处是,您不必像使用自定义助手来完成所有工作那样担心添加 CI 实例。

回答by Sumit

Create a file with the name of your helper in /application/helpersand add it to the autoload config file/load it manually.

/application/helpers中使用您的助手名称创建一个文件,并将其添加到自动加载配置文件中/手动加载。

E.g. place a file called user_helper.phpin /application/helperswith this content:

例如,在/application/helpers 中放置一个名为user_helper.php的文件,其中包含以下内容:

<?php
  function pre($var)
  {
    echo '<pre>';
    if(is_array($var)) {
      print_r($var);
    } else {
      var_dump($var);
    }
    echo '</pre>';
  }
?> 

Now you can either load the helper via $this->load->helper(‘user');or add it to application/config/autoload.phpconfig.

现在您可以通过加载帮助程序$this->load->helper(‘user');或将其添加到application/config/autoload.php配置。

回答by Khurshid Alam Jony

Just define a helper in application helper directory then call from your controller just function name like

只需在应用程序助手目录中定义一个助手,然后从您的控制器中调用函数名称即可

helper name = new_helper.php
function test_method($data){
 return $data
}   

in controller load the helper

在控制器中加载助手

$this->load->new_helper();
$result =  test_method('Hello world!');
if($result){
 echo $result
}

output will be

输出将是

Hello World!

回答by user6280268

To retrieve an item from your config file, use the following function:

要从您的配置文件中检索项目,请使用以下函数:

$this->config->item('item name');Where item name is the $config array index you want to retrieve. For example, to fetch your language choice you'll do this:

$this->config->item('item name');其中 item name 是您要检索的 $config 数组索引。例如,要获取您的语言选择,您将执行以下操作:

$lang = $this->config->item('language');The function returns FALSE (boolean) if the item you are trying to fetch does not exist.

$lang = $this->config->item('language');如果您尝试获取的项目不存在,该函数将返回 FALSE(布尔值)。

If you are using the second parameter of the $this->config->load function in order to assign your config items to a specific index you can retrieve it by specifying the index name in the second parameter of the $this->config->item() function. Example:

如果您使用 $this->config->load 函数的第二个参数将您的配置项分配给特定索引,您可以通过在 $this->config- 的第二个参数中指定索引名称来检索它> item() 函数。例子:

// Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"

// 加载名为 blog_settings.php 的配置文件并将其分配给名为“blog_settings”的索引

$this->config->load('blog_settings', TRUE);

// Retrieve a config item named site_name contained within the blog_settings array

// 检索包含在 blog_settings 数组中的名为 site_name 的配置项

$site_name = $this->config->item('site_name', 'blog_settings');

// An alternate way to specify the same item:

// 指定相同项目的另一种方法:

$blog_config = $this->config->item('blog_settings');

$site_name = $blog_config['site_name'];

$site_name = $blog_config['site_name'];