Laravel:引入自定义类/库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24568008/
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 : Introducing custom Classes/Libraries
提问by Kamran Ahmed
I have just started learning Laravel and during the process, I found out that we can introduce our custom classes into Laravel using the following:
我刚刚开始学习 Laravel,在这个过程中,我发现我们可以使用以下方法将我们的自定义类引入 Laravel:
- Create a folder say
app/MyLib
- Create my class inside
app/MyLib
, say I createdMyDates
Now modify the
ClassLoader::addDirectories
inside theapp/start/global.php
as follows:ClassLoader::addDirectories(array( ... app_path().'/MyLib' ));
Access
MyDates
class, however I want
- 创建一个文件夹说
app/MyLib
- 在里面创建我的类
app/MyLib
,比如说我创建的MyDates
现在修改
ClassLoader::addDirectories
里面的app/start/global.php
内容如下:ClassLoader::addDirectories(array( ... app_path().'/MyLib' ));
访问
MyDates
类,但是我想要
I then came across this article Laravel 4 Application Setup: App library, Autoloading, Bindingthat uses composer
to autoload the custom libraries. Now the question is, what's the best way to introduce my custom libraries in Laravel i.e. what's the recommended approad and if there are any differences between these approaches, what are those?
然后我看到了这篇文章Laravel 4 Application Setup: App library, Autoloading, Bindingthat usedcomposer
to autoload the custom libraries。现在的问题是,在 Laravel 中引入我的自定义库的最佳方法是什么,即推荐的方法是什么,如果这些方法之间有任何区别,那些是什么?
回答by Sebastian Kraft
its best practice and the only way you should do it, if you modify your composer.json as follows
如果您按如下方式修改 composer.json,它的最佳实践也是您应该这样做的唯一方法
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
/* HERE YOUR LIBRARY FOLDER */
"app/MyLib",
]
},
EDIT:You should run after the change composer dump-autoload
to autoload your changes
编辑:您应该在更改后运行composer dump-autoload
以自动加载您的更改