插件demo 添加最新产品列表

This commit is contained in:
Edward Yang 2022-07-21 10:07:29 +08:00
parent 66a6360105
commit e8d1fd1942
8 changed files with 109 additions and 20 deletions

View File

@ -90,6 +90,18 @@ function shop_route($route, $params = []): string
return route('shop.' . $route, $params);
}
/**
* 获取插件链接
*
* @param $route
* @param mixed $params
* @return string
*/
function plugin_route($route, $params = []): string
{
return route('plugin.' . $route, $params);
}
/**
* 是否为当前访问路由
*

View File

@ -117,7 +117,8 @@ class ProductRepo
return $builder;
}
public static function list($data)
public static function list($data=[])
{
return self::getBuilder($data)->paginate($data['per_page'] ?? 20);
}

View File

@ -12,6 +12,7 @@
namespace Beike\Shop\Providers;
use Beike\Plugin\Manager;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
class PluginServiceProvider extends ServiceProvider
@ -39,6 +40,8 @@ class PluginServiceProvider extends ServiceProvider
{
$plugins = $manager->getPlugins();
$bootstraps = $manager->getEnabledBootstraps();
$pluginBasePath = base_path('plugins');
foreach ($bootstraps as $bootstrap) {
$filePath = $bootstrap['file'];
$pluginCode = $bootstrap['code'];
@ -47,6 +50,14 @@ class PluginServiceProvider extends ServiceProvider
$className = "Plugin\\{$pluginCode}\\Bootstrap";
(new $className)->boot();
}
$this->loadViewsFrom("{$pluginBasePath}/{$pluginCode}/Views", $pluginCode);
Route::prefix('plugin')
->middleware('web')
->group(function () use ($pluginBasePath, $pluginCode) {
$this->loadRoutesFrom("{$pluginBasePath}/{$pluginCode}/routes.php");
});
}
}
}

View File

@ -31,24 +31,16 @@ class ShopServiceProvider extends ServiceProvider
$this->mergeConfigFrom(__DIR__ . '/../../Config/beike.php', 'beike');
$this->registerGuard();
$this->app->bind('view.finder', function ($app) {
$paths = $app['config']['view.paths'];
if ($theme = setting('base.theme')) {
$customTheme[] = base_path("themes/{$theme}");
$paths = array_merge($customTheme, $paths);
}
return new FileViewFinder($app['files'], $paths);
});
$this->loadThemeViewPath();
$this->loadComponents();
$this->app->booted(function () {
$this->loadShareViewData();
});
$this->loadViewComponentsAs('shop', [
'sidebar' => AccountSidebar::class,
]);
}
protected function registerGuard()
{
Config::set('auth.guards.' . Customer::AUTH_GUARD, [
@ -62,16 +54,32 @@ class ShopServiceProvider extends ServiceProvider
]);
}
protected function loadThemeViewPath()
{
$this->app->singleton('view.finder', function ($app) {
$paths = $app['config']['view.paths'];
if ($theme = setting('base.theme')) {
$customTheme[] = base_path("themes/{$theme}");
$paths = array_merge($customTheme, $paths);
}
return new FileViewFinder($app['files'], $paths);
});
}
protected function loadComponents()
{
$this->loadViewComponentsAs('shop', [
'sidebar' => AccountSidebar::class,
]);
}
protected function loadShareViewData()
{
View::share('design', request('design') == 1);
View::share('languages', languages());
View::share('shop_base_url', shop_route('home.index'));
$menuCategories = CategoryRepo::getTwoLevelCategories();
View::share('categories', Eventy::filter('header.categories', $menuCategories));
View::share('shop_base_url', shop_route('home.index'));
}
}

View File

@ -18,11 +18,11 @@ class Bootstrap
add_filter('header.categories', function ($data) {
$data[] = [
'name' => '插件链接',
'url' => 'https://www.google.com',
'url' => shop_route('home.index'),
'children' => [
[
"name" => "Google",
"url" => "https://www.google.com",
"name" => "最新商品",
"url" => plugin_route('latest_products'),
], [
"name" => "百度",
"url" => "https://www.baidu.com",

View File

@ -0,0 +1,24 @@
<?php
/**
* MenusController.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-21 10:00:25
* @modified 2022-07-21 10:00:25
*/
namespace Plugin\HeaderMenu\Controllers;
use Beike\Repositories\ProductRepo;
use Beike\Shop\Http\Controllers\Controller;
class MenusController extends Controller
{
public function latestProducts()
{
$products = ProductRepo::list();
return view("HeaderMenu::latest_products", ['products' => $products]);
}
}

View File

@ -0,0 +1,18 @@
@extends('layout.master')
@section('content')
<div class="container">
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active" aria-current="page">Library</li>
</ol>
</nav>
<div class="row">
@foreach ($products as $product)
<div class="col-6 col-md-3">@include('shared.product')</div>
@endforeach
</div>
</div>
@endsection

View File

@ -0,0 +1,15 @@
<?php
/**
* route.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-07-21 09:35:05
* @modified 2022-07-21 09:35:05
*/
use Illuminate\Support\Facades\Route;
Route::get('/latest_products', '\Plugin\HeaderMenu\Controllers\MenusController@latestProducts')->name('plugin.latest_products');