[英]How to autoload helper functions in codeigniter 4
I just downloaded CodeIgniter 4 from their official GitHub.我刚刚从他们的官方 GitHub 下载了 CodeIgniter 4。 They changed a lot from CodeIgniter 3. I want to use
base_url()
function in the view and for that, you need to load URL helper and in CodeIgniter 3 I autoloaded it in config/autoload.php
file.它们与 CodeIgniter 3 相比发生了很大变化。我想在视图中使用
base_url()
函数,为此,您需要加载 URL 帮助程序,并且在 CodeIgniter 3 中我将它自动加载到config/autoload.php
文件中。 But now they have entirely changed the structure of config/autoload.php
file in CodeIgniter 4 and it is very confusing to me.但是现在他们完全改变了 CodeIgniter 4 中
config/autoload.php
文件的结构,这让我很困惑。
You can still use the base_url()
function in your views in CodeIgniter 4 by using below code in your constructor of controller helper('url');
您仍然可以在 CodeIgniter 4 的视图中使用
base_url()
函数,方法是在控制器helper('url');
构造函数中使用以下代码helper('url');
If anybody who used CodeIgnter 4 knows how to autoload helper functions like url by modifying config/autoload.php
file please help me.如果任何使用 CodeIgnter 4 的人知道如何通过修改
config/autoload.php
文件来自动加载帮助函数,如 url,请帮助我。
CodeIgnter 4 is currently in developing phase so many features are still not available. CodeIgnter 4 目前处于开发阶段,因此许多功能仍然不可用。 The answer is you cannot autoload helpers or libraries in autoload.php file in codeigniter 4.
答案是你不能在 codeigniter 4 的 autoload.php 文件中自动加载助手或库。
I know this is a feature used by many of us but autoloading every thing will decrease site performance so may be developer team decided to drop this feature.我知道这是我们许多人使用的功能,但自动加载每件事都会降低网站性能,因此开发团队可能决定放弃此功能。
from Codeigniter 4 Documentation:来自 Codeigniter 4 文档:
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.
您可以在控制器构造函数中加载您的助手,以便它们在任何函数中自动可用,或者您可以在需要它的特定函数中加载助手。
https://bcit-ci.github.io/CodeIgniter4/general/helpers.html https://bcit-ci.github.io/CodeIgniter4/general/helpers.html
将您的助手添加到BaseController.php
文件中的数组,如下所示:
protected $helpers = ["form"] // BaseController.php:29;
Since there is no autoload in autoload.php
you have to load all helper files by itself.由于 autoload.php 中没有自动
autoload.php
您必须自己加载所有帮助文件。 Unlike global load in CodeIgniter 3.与 CodeIgniter 3 中的全局加载不同。
How to load如何加载
public function FunctionName()
{
helper(['form', 'validation']); # before retun to view.
#rest of your code
}
Do we need to load this into each and every method(s)?我们是否需要将其加载到每个方法中?
Yes.是的。 Since I play around it I'm unable to load helper file globally.(2018-2-07)
由于我玩弄它,我无法全局加载帮助文件。(2018-2-07)
Just add name of helper(s) you want to load in只需添加您要加载的助手的名称
protected $helpers = []
in protected $helpers = []
in
/app/Controllers/BaseController.php
CodeIgniter4 will load those helpers automatically. CodeIgniter4 将自动加载这些帮助程序。
For example:例如:
class BaseController extends Controller
{
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = ['cookie','date']; // <=== this
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.