如何在 CodeIgniter 4 中创建和加载新的帮助程序

M. *_*san 3 php codeigniter codeigniter-4

我想在助手中创建新函数,但它仍然失败: Call to undefined function

app/Helper/Text_helper.php我在使用中保存我的助手namespace App\Helpers; 并使用 BaseController 加载助手protected $helpers = ['text'];

参考: https: //codeigniter4.github.io/userguide/general/helpers.html#extending-helpers 但它仍然不起作用

小智 7

文档中没有提及,但请记住在助手的文件名中添加后缀_helper,否则它将无法在 codeigniter 4 中工作。

例如,如果您创建了一个助手xxx.php,请将其更改为xxx_helper.php

要加载助手,您可以使用助手函数(例如:)helper('xxx.php');或将其添加到$helpers数组中,该数组是BaseController中的受保护属性