Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

481
Vistas
laravel PHP Fatal error: Cannot redeclare for function

I have the following function :

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;
}

I have this function declare in two blades and I got this error :

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

How can I fix it?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Here a smart simple trick

add a file called helpers.php into your app folder

copy the function you wrote inside that file

add to composer.json under "autoload" the block:

 "files": [
  "app/helpers.php"
]

then run a composer update and you'll get the function available on all of your codebase

In this file you can add any helper functions you like, and as has already been told you should always avoid to write php code inside views

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda