我有以下功能:
function get_user_browser() {
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$ub = 'unknown';
if (preg_match('/Trident\/7.0; rv:11.0/', $u_agent)) {
$ub = "ie ie-11";
}
elseif(preg_match('/MSIE/i', $u_agent)) {
$ub = "ie";
}
elseif(preg_match('/Chrome/i', $u_agent)) {
$ub = "chrome";
}
elseif(preg_match('/Firefox/i', $u_agent)) {
$ub = "firefox";
}
elseif(preg_match('/Safari/i', $u_agent)) {
$ub = "safari";
}
elseif(preg_match('/Opera/i', $u_agent)) {
$ub = "opera";
}
return 'browser-'.$ub;
}
我在两个 Blade 中声明了这个函数,我收到了这个错误:
FPHP Fatal error: Cannot redeclare get_user_browser() (previously declared in /home/storage/framework/views/0800146a0e35b205e9a66bb2f00ffb2f:7) in /home/storage/framework/views/0800146a0e35b205e9a66bb2f00ffb2f on line 7
我该如何解决?
最佳答案
这是一个聪明的简单技巧
将一个名为 helpers.php 的文件添加到您的应用程序文件夹中
复制您在该文件中编写的函数
在“自动加载”块下添加到 composer.json:
"files": [
"app/helpers.php"
]
然后运行 Composer 更新,您将获得所有代码库上可用的功能
在这个文件中,你可以添加任何你喜欢的辅助函数,正如已经说过的,你应该始终避免在 View 中编写 php 代码
关于php - Laravel PHP fatal error : Cannot redeclare for function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43513053/