laravel - 如何在自己的 laravel 包中添加助手?(调用未定义的函数)
问题描述
在我的 composer.json 我写过:
"autoload": {
"psr-4": {
"Pmochine\\MyOwnPackage\\": "src/"
},
"files": [
"src/helpers.php"
]
},
但不知何故,即使在 composer dump-autoload 之后,函数也没有加载。我得到“调用未定义的函数”。为了创建包,我使用了包生成器。也许它与在供应商文件夹中创建一个符号链接有关?
我写过的内部助手
<?php
if (! function_exists('myowntest')) {
function myowntest()
{
return 'test';
}
}
解决方案
在包服务提供者中,尝试添加:
// if 'src/helpers.php' does not work, try with 'helpers.php'
if (file_exists($file = app_path('src/helpers.php'))) {
require $file;
}
推荐阅读
- python - 超出箱线图的点是否为异常值?
- html - 如何使用 CSS 将两种不同不透明度的颜色混合在一起
- javascript - 想要通过单击菜单元素外部来隐藏菜单
- excel - 使用 Excel VBA 创建 Word 文档 - 插入图像?从 3rd Doc 复制和粘贴文本
- python - 使用替代输入再次调用函数的大多数 Pythonic 方式
- javascript - iOs Safari 上的 document.body.insertBefore 无法正常工作
- r - Grep 并根据这些值创建新的数据框
- python - 合并文本分类的多种统计方法,从 SVM 多类分类器开始
- asp.net-mvc - MVC:Ajax.ActionLink 是否可以使用图像和图像右侧的文本?
- vba - 如何在代码中将数组函数与数组一起使用