ryndshn
ryndshn

Reputation: 701

require() not working in Laravel 5

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.

enter image description here

I am still rather new to the PHP and Laravel world, so if it's something simple I apologize.

Upvotes: 2

Views: 18460

Answers (2)

wiesson
wiesson

Reputation: 6822

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

Upvotes: 6

Maksym
Maksym

Reputation: 3428

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;

Upvotes: 1

Related Questions