php Laravel:如何在 Laravel 中包含来自供应商文件夹的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21671816/
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
Laravel: How to include file from Vendor folder in Laravel
提问by Growlithe
I am trying to include the YouTube Analytics Service of Google but I can not access it through the Vendor folder.
我正在尝试包含 Google 的 YouTube 分析服务,但无法通过 Vendor 文件夹访问它。
include(app_path.'path/to/analytics/Google_YoutubeAnalyticsService.php')
It is not working, because it defaults to the App folder.
它不起作用,因为它默认为 App 文件夹。
How can I get out of the App folder and into the Vendor folder (where the YouTube Analytics file is at)?
如何退出 App 文件夹并进入 Vendor 文件夹(YouTube 分析文件所在的位置)?
The error is {
错误是{
include(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php): failed to open stream: No such file or directory
包括(C:\xampp\htdocs\mysite\app/path/to/analytics/Google_YoutubeAnalyticsService.php):无法打开流:没有这样的文件或目录
回答by Miroslav Trninic
From where do you want to include that file ?
您想从哪里包含该文件?
Place a reference to your file in composer.jsonautoload object:
在composer.json自动加载对象中放置对您的文件的引用:
"autoload": {
"files":["your_file_path"]
}
Run composer dumpautoload, and you'll have your file :)
运行 composer dumpautoload,你就会得到你的文件 :)
回答by rebduvid
Actually you have in the helpers function the path so basically the function base_path give the direction to the root of your project so
实际上,您在 helpers 函数中具有路径,因此基本上函数 base_path 为项目的根提供了方向,因此
echo base_path() . '/vendor';
Should be the route to your vendor folder.
应该是到您的供应商文件夹的路径。
You can se all the documentation in Helper Functions Laravel
您可以在Helper Functions Laravel 中查看所有文档
Be sure that you are seeing the documentation of the laravel version that you are using (I put the link for the 4.2 version).
确保您看到的是您正在使用的 laravel 版本的文档(我提供了 4.2 版本的链接)。
回答by Goddard
This question was asked a long time ago and the answers reflect that. Most the time now all you need to do is import it using the "use" statement if you installed it with composer. Composer will already reference all the important directories.
这个问题是很久以前提出的,答案反映了这一点。大多数时候你需要做的就是使用“use”语句导入它,如果你用composer安装了它。Composer 已经引用了所有重要的目录。
It should be something like this, but it will vary depending on the project.
它应该是这样的,但它会因项目而异。
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\Class;
That could include a base class as well as some exception classes.
这可能包括一个基类以及一些异常类。
use FolderNameUsuallyGitHubUserName\ClassNameorGitHubProjectName\ClassException;
Usually most packages if compliant with modern composer and php standards work in this fashion.
通常大多数符合现代 composer 和 php 标准的包都以这种方式工作。

