模块资源
模块资源
你的模块很可能包含laravel调用的资源,这些资源包含配置、视图、翻译文件等。
为了让你的模块正确加载并且如果想发布它们,你需要让laravel知道它们,就像在任何常规包中一样。
注意
这些资源被加载到由模块生成的服务提供者中(使用module:make
),除非使用plain
标志,在这种情况下,你需要自己处理这个逻辑。
注意
不要忘记更改路径,在下面的代码片段中,假定使用“Blog”模块。
配置
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('blog.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../Config/config.php', 'blog'
);
Views
$viewPath = base_path('resources/views/modules/blog');
$sourcePath = __DIR__.'/../Resources/views';
$this->publishes([
$sourcePath => $viewPath
]);
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/blog';
}, \Config::get('view.paths')), [$sourcePath]), 'blog');
这里的主要部分是 loadViewsFrom
方法调用。如果你不希望你的视图被发布到 laravel views 文件夹中,你可以删除对 $this->publisher()
的调用。
语言文件
$langPath = base_path('resources/lang/modules/blog');
if (is_dir($langPath)) {
$this->loadTranslationsFrom($langPath, 'blog');
} else {
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'blog');
}
在blade中使用 {{__(blog::foo)}}
将在以下路径中搜索:/lang/modules/en/foo.php
/Modules/Blog/Resources/lang/en/foo.php
工厂
如果你想使用laravel工厂,你必须在你的服务提供者中添加以下内容:
$this->app->singleton(Factory::class, function () {
return Factory::construct(__DIR__ . '/Database/factories');
});