Support the ongoing development of →
Packages Architecture
Last updated 1 year ago.

Cannot redeclare means either your include is being done more than once (solution: use include_once()) or that a class with that name already exists. To prevent that the namespaces exist. Also files in Laravel will automatically be loaded depending on namespaces by PSR-4 so if your class file is in the right place, is correctly named and has the right namespace you shouldn't have to worry about it.



Indeed i am trying to include the Mobile_Detect library( In my controller i tried include_once() and got cannot redeclare, but note, if i only put this line of code:

$detect = new Mobile_Detect;

AND put the Mobile_Detect.php file in the same directory as the controller, i got cannot redeclare too...

Any idea?


Easy way: place the file somewhere in you app folder. Autoload it as file on your composer.json Not so Hard way: create a service provide to load the class(still you need to autoload your class)


I created a directory and added the file at:


updated composer.json after the "classmap" in the autoload section with:

"files": [ "app/library/Mobile_Detect.php" ],

and added in MyController.php file:

use Mobile_Detect;

It worked! Thanks!


Sign in to participate in this thread!


Your banner here too?

Gittinhub gittinhub Joined 13 Nov 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.