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

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.

0

Thanks.

Indeed i am trying to include the Mobile_Detect library(mobiledetect.net). 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?

0

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)

0

I created a directory and added the file at:

/app/library/

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!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Gittinhub gittinhub Joined 13 Nov 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.