laravel 如何在laravel 4中自动加载“库”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17584810/
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
How to autoload 'libraries' in laravel 4?
提问by Miguel Borges
I've created a library folder in app folder to add my app libraries. I've updated app config file and composer.json to autoload that folder, but when I run the command composer dump-autoload
I get the next error:
我在 app 文件夹中创建了一个库文件夹来添加我的应用程序库。我已更新应用程序配置文件和 composer.json 以自动加载该文件夹,但是当我运行该命令时composer dump-autoload
,出现下一个错误:
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'App\\Libraries\\Search\\SearchServiceProvider' not found","file":"D:\\Users\\Miguel Borges\\Documents\\Trabalhos\\Tese\\portal\\bootstrap\\compiled.php","line":4130}}PHP Fatal error: Class 'App\Libraries\Search\SearchServiceProvider' not found in D:\Users\Miguel Borges\Documents\Trabalhos\Tese\portal\bootstrap\compiled.php on line 4130 [Finished in 1.1s with exit code 255]
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'App\\Libraries\\Search\\SearchServiceProvider' not found","file":"D:\\Users\\Miguel Borges\\Documents\\Trabalhos\\Tese\\portal\\bootstrap\\compiled.php","line":4130}}PHP Fatal error: Class 'App\Libraries\Search\SearchServiceProvider' not found in D:\Users\Miguel Borges\Documents\Trabalhos\Tese\portal\bootstrap\compiled.php on line 4130 [Finished in 1.1s with exit code 255]
My app folder tree:
我的应用程序文件夹树:
app
| ...
+ libraries
| + search
| | - Search.php
| | - SearchFacade.php
| | - SearchServiceProvider.php
| + lib2
| | - ...
| + lib3
| | - ...
| | - Theme.php
| - ...
- filters.php
- routes.php
SearchServiceProvider.php
搜索服务提供者.php
namespace App\Libraries\Search;
use Illuminate\Support\ServiceProvider;
class SearchServiceProvider extends ServiceProvider {
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['search'] = $this->app->share(function($app)
{
return new Search;
});
}
}
composer.json
作曲家.json
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/libraries",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
// ,
// "psr-0": {
// "app": "app/libraries"
// }
},
Basically, I need to autoload all the libraries within the 'libraries' folder.
基本上,我需要自动加载“库”文件夹中的所有库。
回答by fideloper
You should create a top-level namespace for your application.
您应该为您的应用程序创建一个顶级命名空间。
Then put all libraries youcode under that namespace. Note:Any third-party libraries should (hopefully) be installed via Composer and therefore have its own namespace/autoloading setup.
然后将您编码的所有库放在该命名空间下。注意:任何第三方库都应该(希望)通过 Composer 安装,因此有自己的命名空间/自动加载设置。
Your directory structure would then be:
您的目录结构将是:
libraries
Myapp
Search (note directory is capitalized)
Search.php
SearchFacade.php
SearchServiceProvider.php
AnotherLib
Then your classes will follow that namespace:
然后你的类将遵循该命名空间:
File: Myapp/Search/Search.php
:
文件Myapp/Search/Search.php
::
<?php namespace Myapp\Search;
class Search { ... }
And finally, your autoloading setup:
最后,您的自动加载设置:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/libraries",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
,
"psr-0": {
"Myapp": "app/libraries"
}
},