From bf632700a0a371d0b489480dda0dbdeb7c99b81d Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 1 Feb 2023 10:03:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9D=A2=E5=8C=85=E5=B1=91li?= =?UTF-8?q?brary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- beike/Helpers.php | 4 +- beike/Libraries/Breadcrumb.php | 69 +++++++++++++++++++ .../Shop/Http/Controllers/BrandController.php | 4 ++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 beike/Libraries/Breadcrumb.php diff --git a/beike/Helpers.php b/beike/Helpers.php index afa59ff7..1b3c0d6f 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -154,9 +154,9 @@ function type_route($type, $value): string } elseif ($type == 'custom') { if (Str::startsWith($value, ['http://', 'https://'])) { return $value; - } else { - return "//{$value}"; } + + return "//{$value}"; } return ''; diff --git a/beike/Libraries/Breadcrumb.php b/beike/Libraries/Breadcrumb.php new file mode 100644 index 00000000..79f07516 --- /dev/null +++ b/beike/Libraries/Breadcrumb.php @@ -0,0 +1,69 @@ + + * @created 2023-02-01 09:38:33 + * @modified 2023-02-01 09:38:33 + */ + +namespace Beike\Libraries; + +use Illuminate\Contracts\View\View; + +class Breadcrumb +{ + public array $breadcrumbs; + + /** + * Create a new component instance. + * + * @return void + * @throws \Exception + */ + public function __construct() + { + $this->breadcrumbs[] = [ + 'title' => trans('shop/common.home'), + 'url' => shop_route('home.index'), + ]; + } + + /** + * 获取 Breadcrumb 实例 + * @return Breadcrumb + */ + public static function getInstance(): self + { + return new self; + } + + /** + * 添加面包屑节点链接 + * + * @param $url + * @param $text + * @return Breadcrumb + */ + public function addLink($url, $text): self + { + $this->breadcrumbs[] = [ + 'url' => $url, + 'title' => $text, + ]; + + return $this; + } + + /** + * Get the view / contents that represent the component. + * + * @return View| + */ + public function render(): View + { + return view('components.breadcrumbs', ['breadcrumbs' => collect($this->breadcrumbs)]); + } +} diff --git a/beike/Shop/Http/Controllers/BrandController.php b/beike/Shop/Http/Controllers/BrandController.php index bd0b5f99..a7838da2 100644 --- a/beike/Shop/Http/Controllers/BrandController.php +++ b/beike/Shop/Http/Controllers/BrandController.php @@ -21,6 +21,10 @@ class BrandController extends Controller public function show(int $id) { $brand = BrandRepo::find($id); + if (empty($brand)) { + return redirect(shop_route('brands.index')); + } + $products = $brand->products() ->with([ 'masterSku',