require() 在 Laravel 5 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28938641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
require() not working in Laravel 5
提问by Ryan DuShane
I am using the basic Laravel 5 file structure.
我正在使用基本的 Laravel 5 文件结构。
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.
我基本上在 app 目录中有一些 .php 文件。我试图从 app/Http/Controllers 目录中的控制器调用它们。
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.
这是我的控制器中出现故障的两条线。我的 IDE PhpStorm 显示它可以识别目录。但是它抛出了一个 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
.
这是文件结构的照片。如果您不熟悉 Laravel 5。上面的 require 函数是从内调用的PlaylistController.php
。
I am still rather new to the PHP and Laravel world, so if it's something simple I apologize.
我对 PHP 和 Laravel 世界仍然很陌生,所以如果这很简单,我深表歉意。
回答by wiesson
Instead of the require function, you could make use of namespaces. Assuming you have some model files for your Controller:
您可以使用命名空间来代替 require 函数。假设您的控制器有一些模型文件:
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'):
如果您有自己的类,您可以将它们放在您自己的文件夹 app/Libraries 中并与它们一起使用(例如“Libraries”):
use App\Libraries\Playlist;
and within your Controller:
并在您的控制器中:
$play = Playlist::find(1);
or directly:
或直接:
$play = App\Libraries\Playlist::find(1);
回答by Maksym Cierzniak
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
或者,如果这些是类,那么你应该在 App 命名空间中命名它们,然后像下面的答案说使用
use App/Playlist;