我在 App\Helpers.php 中有一个自定义函数。当我在 Blade 文件中的 if 语句中使用该函数时。我在 Laragon 错误日志中看到错误。
PHP Fatal error: Cannot redeclare CheckInvalidPlan() (previously declared in C:\laragon\www\projectname\app\Helpers.php:6) in C:\laragon\www\projectname\app\Helpers.php on line 6
Run Code Online (Sandbox Code Playgroud)
然而事情按预期进行。但为什么会导致这个错误以及如何修复它?
#更新
这是我在 Helpers.php 中的函数
function CheckInvalidPlan($id)
{
if (Plan::find($id) == null)
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我在控制器中的 if 语句。
if (CheckInvalidPlan ($request->plan_id))
{
return back()->with('invalid', 'Invalid membership plan spesified.');
}
Run Code Online (Sandbox Code Playgroud)
您可以通过检查您的函数是否已存在来绕过此错误:
if(! function_exists('CheckInvalidPlan')) {
function CheckInvalidPlan($id)
{
if (Plan::find($id) == null)
{
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是 Laravel 助手的声明方式:
if (! function_exists('today')) {
/**
* Create a new Carbon instance for the current date.
*
* @param \DateTimeZone|string|null $tz
* @return \Illuminate\Support\Carbon
*/
function today($tz = null)
{
return Date::today($tz);
}
}
Run Code Online (Sandbox Code Playgroud)
然而,更简洁的方法是了解为什么帮助程序文件被加载两次。
很难准确地告诉您错误可能在哪里,但是您应该检查所有类,app\Helpers.php永远不应该手动需要该文件。它应该由作曲家自动加载,如本答案中所述(感谢 N69S)。
| 归档时间: |
|
| 查看次数: |
3057 次 |
| 最近记录: |