支持插件多语言
This commit is contained in:
parent
4721cb49b3
commit
8cf90c601c
|
|
@ -17,6 +17,8 @@ use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class PluginServiceProvider extends ServiceProvider
|
class PluginServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
|
private string $pluginBasePath = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the application services.
|
* Register the application services.
|
||||||
*
|
*
|
||||||
|
|
@ -36,30 +38,74 @@ class PluginServiceProvider extends ServiceProvider
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$manager = app('plugin');
|
$manager = app('plugin');
|
||||||
$plugins = $manager->getPlugins();
|
|
||||||
$bootstraps = $manager->getEnabledBootstraps();
|
$bootstraps = $manager->getEnabledBootstraps();
|
||||||
$pluginBasePath = base_path('plugins');
|
$this->pluginBasePath = base_path('plugins');
|
||||||
|
|
||||||
foreach ($bootstraps as $bootstrap) {
|
foreach ($bootstraps as $bootstrap) {
|
||||||
$filePath = $bootstrap['file'];
|
|
||||||
$pluginCode = $bootstrap['code'];
|
$pluginCode = $bootstrap['code'];
|
||||||
if (file_exists($filePath)) {
|
$this->bootPlugin($bootstrap);
|
||||||
$className = "Plugin\\{$pluginCode}\\Bootstrap";
|
$this->loadRoutes($pluginCode);
|
||||||
if (method_exists($className, 'boot')) {
|
$this->loadTranslations($pluginCode);
|
||||||
(new $className)->boot();
|
$this->loadViews($pluginCode);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$routePath = "{$pluginBasePath}/{$pluginCode}/routes.php";
|
|
||||||
if (file_exists($routePath)) {
|
|
||||||
Route::prefix('plugin')
|
|
||||||
->middleware('web')
|
|
||||||
->group(function () use ($routePath) {
|
|
||||||
$this->loadRoutesFrom($routePath);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用插件 Bootstrap::boot()
|
||||||
|
*
|
||||||
|
* @param $bootstrap
|
||||||
|
*/
|
||||||
|
private function bootPlugin($bootstrap)
|
||||||
|
{
|
||||||
|
$filePath = $bootstrap['file'];
|
||||||
|
$pluginCode = $bootstrap['code'];
|
||||||
|
if (file_exists($filePath)) {
|
||||||
|
$className = "Plugin\\{$pluginCode}\\Bootstrap";
|
||||||
|
if (method_exists($className, 'boot')) {
|
||||||
|
(new $className)->boot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载插件路由
|
||||||
|
*
|
||||||
|
* @param $pluginCode
|
||||||
|
*/
|
||||||
|
private function loadRoutes($pluginCode)
|
||||||
|
{
|
||||||
|
$pluginBasePath = $this->pluginBasePath;
|
||||||
|
$routePath = "{$pluginBasePath}/{$pluginCode}/routes.php";
|
||||||
|
if (file_exists($routePath)) {
|
||||||
|
Route::prefix('plugin')
|
||||||
|
->middleware('web')
|
||||||
|
->group(function () use ($routePath) {
|
||||||
|
$this->loadRoutesFrom($routePath);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载多语言
|
||||||
|
*/
|
||||||
|
private function loadTranslations($pluginCode)
|
||||||
|
{
|
||||||
|
$pluginBasePath = $this->pluginBasePath;
|
||||||
|
$this->loadTranslationsFrom("{$pluginBasePath}/{$pluginCode}/Lang", $pluginCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载模板目录
|
||||||
|
*
|
||||||
|
* @param $pluginCode
|
||||||
|
*/
|
||||||
|
private function loadViews($pluginCode)
|
||||||
|
{
|
||||||
|
$pluginBasePath = $this->pluginBasePath;
|
||||||
|
$this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,14 @@ class Bootstrap
|
||||||
{
|
{
|
||||||
add_filter('header.categories', function ($data) {
|
add_filter('header.categories', function ($data) {
|
||||||
$data[] = [
|
$data[] = [
|
||||||
'name' => '插件链接',
|
'name' => trans('HeaderMenu::header.plugin_link'),
|
||||||
'url' => shop_route('home.index'),
|
'url' => shop_route('home.index'),
|
||||||
'children' => [
|
'children' => [
|
||||||
[
|
[
|
||||||
"name" => "最新商品",
|
"name" => trans('HeaderMenu::header.latest_products'),
|
||||||
"url" => plugin_route('latest_products'),
|
"url" => plugin_route('latest_products'),
|
||||||
], [
|
], [
|
||||||
"name" => "百度",
|
"name" => trans('HeaderMenu::header.baidu'),
|
||||||
"url" => "https://www.baidu.com",
|
"url" => "https://www.baidu.com",
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* dd.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-07-28 16:19:06
|
||||||
|
* @modified 2022-07-28 16:19:06
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [
|
||||||
|
'plugin_link' => 'Plugin Link',
|
||||||
|
'latest_products' => 'Latest Products',
|
||||||
|
'baidu' => 'Baidu',
|
||||||
|
];
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* dd.php
|
||||||
|
*
|
||||||
|
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||||
|
* @link http://www.guangdawangluo.com
|
||||||
|
* @author Edward Yang <yangjin@opencart.cn>
|
||||||
|
* @created 2022-07-28 16:19:06
|
||||||
|
* @modified 2022-07-28 16:19:06
|
||||||
|
*/
|
||||||
|
|
||||||
|
return [
|
||||||
|
'plugin_link' => '插件链接',
|
||||||
|
'latest_products' => '最新商品',
|
||||||
|
'baidu' => '百度',
|
||||||
|
];
|
||||||
Loading…
Reference in New Issue