如何在Laravel中将包视图添加到主视图? [英] How to add package views to master view in Laravel?
问题描述
好的,所以我在Laravel项目中构建了多个软件包,但是每个软件包都有一些管理方面的内容,例如博客和论坛.现在我有了我的主要项目,除了为所有软件包提供纯基础之外,它所要做的只是其他事情.
Alright so I'm building multiple packages in my Laravel project, however each package has some administration side with it for example blogs and forums. Now I got my main project, which doesn't do more than supply a pure foundation for all packages.
我有自己的主导航管理模板,现在我在想,是否有办法从软件包中动态加载此菜单的扩展名?这是整个结构:
I got my admin template with it's own main navigation, now I was wondering if there would be a way to dynamically load in extensions of this menu from the packages? Here's the structure of the whole thing:
Resources
- views
- cp
- homer //Name of the template
- master.blade.php
- components
- nav.blade.php
现在在我的包裹中,我将具有以下结构
Now inside my package I would have the following structure
views
- cp
- components
- nav.blade.php
将所有具有相同结构的软件包合并到一起,将所有nav.blade.php合并在一起的最佳步骤是什么?
What would be the best step to merge all packages with the same structure to merge all nav.blade.php's together?
推荐答案
结果,我最终创建了自己的服务,如下所示:
As a result I ended up creating my own service as follow:
namespace App\Services;
class ComposerService {
public function getLoadedPackages()
{
$folders = scandir(base_path('packages/author'));
unset($folders[0]);
unset($folders[1]);
unset($folders[2]);
return $folders;
}
}
这将读出我创建自己的软件包的正确文件夹
This will read out the correct folder I create my own packages is
之后,我更新了AppServiceProvider
After that I updated the AppServiceProvider
在启动功能中,我做了以下
in the boot function I did the following
/** @var \App\Services\ComposerService $compiler */
$compiler = new ComposerService();
$packages = $compiler->getLoadedPackages();
$compiled = '';
foreach($packages as $package)
{
$compiled .= view('prefix-'.$package.'::cp.components.nav')->render();
}
View::share('main_nav', $compiled);
现在我可以在刀片中访问main_nav并使用它了
Now I can access main_nav in my blade and use it like
{!! $main_nav !!}
现在它会打印出我对每个软件包的所有导航视图.
It now prints out all my nav views that I have for each package.
这篇关于如何在Laravel中将包视图添加到主视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!