I would lke to know how do i include a myclass.php file ???? Where?
tried to use include('myclass.php') in the controller file but it is saying CANNOT REDECLARE what i only declare once, cause there is only one file beeing incuded. Trying to type it in the controller file by hand gives CANNOT REDECLARE class too. Also, checking ifclass_exists('myclass.php') does not work too.
Can anyone tell me how to include a php file with a class in laravel 5.1
Thanks.
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.
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?
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:
/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!
why not use
https://github.com/jenssegers/agent
or
https://github.com/hisorange/browser-detect
they are specially made for laravel
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community