require() not working in Laravel 5
Solution 1
Instead of the require function, you could make use of namespaces. Assuming you have some model files for your Controller:
use App\Playlist;
use App\PlaylistRetrieval;
If you have your own classes, you could put them in your own folder app/Libraries and use them with (example with 'Libraries'):
use App\Libraries\Playlist;
and within your Controller:
$play = Playlist::find(1);
or directly:
$play = App\Libraries\Playlist::find(1);
Solution 2
Try this:
require (__DIR__.'/../../PlaylistRetrieval.php');
require (__DIR__.'/../../Playlist.php');
Or if this ones are the classes then you should namespace them in App namespace then like the answer below says use
use App/Playlist;
Related videos on Youtube
ryndshn
Updated on June 04, 2022Comments
-
ryndshn almost 2 years
I am using the basic Laravel 5 file structure.
I basically have some .php files inside of the app directory. I am trying to call them from a controller within the app/Http/Controllers directory.
require ('../../PlaylistRetrieval.php'); require ('../../Playlist.php');
These are the two lines that are failing from within my controller. My IDE, PhpStorm, shows that it recognizes the directories. However it is throwing a FatalErrorException.
Here's a photo of the file structure. Incase you aren't familiar with Laravel 5. The require functions above are being called from within
PlaylistController.php
.I am still rather new to the PHP and Laravel world, so if it's something simple I apologize.
-
laka over 4 yearswhy is this rated so badly? +1 for the DIR it worked instantly for me.