在整个应用程序 cakephp 3 中包含实用程序 class

Include utility class all over the application cakephp 3

我有自定义实用程序 class,其中包含一些通用方法,它位于 src/Utility/UtilityClass。

我可以在控制器中包含实用程序 class。例如

use App\Utility\ArrayUtil;

然后在我的控制器中调用 class,例如

ArrayUtil::myMethod();

并且有效。我需要在 bootstrap 中包含 UtilityClass,以便它适用于整个应用程序,这样我也可以在模型、模板和其他控制器中重用它。

我试图在 config/bootstrap.php 中加载它,但出现此错误:

Error: Class 'ArrayUtil' not found

任何想法

您可以在页面顶部添加这一行,无论是模型、视图还是控制器。

 use App\Utility\ArrayUtil;

如果您在多个视图中使用此实用程序,那么我建议您将此行写在 Template/Layout/default.ctp 中,因为所有模板都是其中的一部分。

希望对您有所帮助。

和平! xD