Refactor url link and label.
This commit is contained in:
parent
dff24e9675
commit
68231b0538
|
|
@ -87,9 +87,14 @@ class CategoryController extends Controller
|
|||
|
||||
protected function save(Request $request, ?Category $category = null)
|
||||
{
|
||||
$category = (new CategoryService())->createOrUpdate($request->all(), $category);
|
||||
$requestData = $request->all();
|
||||
$category = (new CategoryService())->createOrUpdate($requestData, $category);
|
||||
$data = [
|
||||
'category' => $category,
|
||||
'request_data' => $requestData,
|
||||
];
|
||||
|
||||
hook_action('admin.category.save.after', $category);
|
||||
hook_action('admin.category.save.after', $data);
|
||||
|
||||
return redirect($this->getRedirect())->with('success', 'Category created successfully');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,13 +6,8 @@ use Beike\Models\AdminUser;
|
|||
use Beike\Models\Currency;
|
||||
use Beike\Models\Customer;
|
||||
use Beike\Models\Language;
|
||||
use Beike\Repositories\BrandRepo;
|
||||
use Beike\Repositories\CategoryRepo;
|
||||
use Beike\Repositories\CurrencyRepo;
|
||||
use Beike\Repositories\LanguageRepo;
|
||||
use Beike\Repositories\PageCategoryRepo;
|
||||
use Beike\Repositories\PageRepo;
|
||||
use Beike\Repositories\ProductRepo;
|
||||
use Beike\Services\CurrencyService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
|
@ -130,39 +125,7 @@ function shop_route($route, $params = []): string
|
|||
*/
|
||||
function type_route($type, $value): string
|
||||
{
|
||||
$types = ['category', 'product', 'brand', 'page', 'page_category', 'order', 'rma', 'static', 'custom'];
|
||||
if (empty($type) || empty($value) || ! in_array($type, $types)) {
|
||||
return '';
|
||||
}
|
||||
if (is_array($value)) {
|
||||
throw new \Exception('Value must be integer, string or object');
|
||||
}
|
||||
|
||||
if ($type == 'category') {
|
||||
return shop_route('categories.show', ['category' => $value]);
|
||||
} elseif ($type == 'product') {
|
||||
return shop_route('products.show', ['product' => $value]);
|
||||
} elseif ($type == 'brand') {
|
||||
return shop_route('brands.show', [$value]);
|
||||
} elseif ($type == 'page') {
|
||||
return shop_route('pages.show', ['page' => $value]);
|
||||
} elseif ($type == 'page_category') {
|
||||
return shop_route('page_categories.show', ['page_category' => $value]);
|
||||
} elseif ($type == 'order') {
|
||||
return shop_route('account.order.show', ['number' => $value]);
|
||||
} elseif ($type == 'rma') {
|
||||
return shop_route('account.rma.show', ['id' => $value]);
|
||||
} elseif ($type == 'static') {
|
||||
return shop_route($value);
|
||||
} elseif ($type == 'custom') {
|
||||
if (Str::startsWith($value, ['http://', 'https://'])) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return "//{$value}";
|
||||
}
|
||||
|
||||
return '';
|
||||
return \Beike\Libraries\Url::getInstance()->link($type, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,34 +138,7 @@ function type_route($type, $value): string
|
|||
*/
|
||||
function type_label($type, $value, array $texts = []): string
|
||||
{
|
||||
$types = ['category', 'product', 'brand', 'page', 'page_category', 'static', 'custom'];
|
||||
if (empty($type) || empty($value) || ! in_array($type, $types)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$locale = locale();
|
||||
$text = $texts[$locale] ?? '';
|
||||
if ($text) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
if ($type == 'category') {
|
||||
return CategoryRepo::getName($value);
|
||||
} elseif ($type == 'product') {
|
||||
return ProductRepo::getName($value);
|
||||
} elseif ($type == 'brand') {
|
||||
return BrandRepo::getName($value);
|
||||
} elseif ($type == 'page') {
|
||||
return PageRepo::getName($value);
|
||||
} elseif ($type == 'page_category') {
|
||||
return PageCategoryRepo::getName($value);
|
||||
} elseif ($type == 'static') {
|
||||
return trans('shop/' . $value);
|
||||
} elseif ($type == 'custom') {
|
||||
return $text;
|
||||
}
|
||||
|
||||
return '';
|
||||
return \Beike\Libraries\Url::getInstance()->label($type, $value, $texts);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -0,0 +1,134 @@
|
|||
<?php
|
||||
/**
|
||||
* Url.php
|
||||
*
|
||||
* @copyright 2023 beikeshop.com - All Rights Reserved
|
||||
* @link https://beikeshop.com
|
||||
* @author Edward Yang <yangjin@guangda.work>
|
||||
* @created 2023-03-15 14:13:39
|
||||
* @modified 2023-03-15 14:13:39
|
||||
*/
|
||||
|
||||
namespace Beike\Libraries;
|
||||
|
||||
use Beike\Repositories\BrandRepo;
|
||||
use Beike\Repositories\CategoryRepo;
|
||||
use Beike\Repositories\PageCategoryRepo;
|
||||
use Beike\Repositories\PageRepo;
|
||||
use Beike\Repositories\ProductRepo;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Url
|
||||
{
|
||||
public const TYPES = [
|
||||
'category', 'product', 'brand', 'page', 'page_category', 'order', 'rma', 'static', 'custom',
|
||||
];
|
||||
|
||||
public static function getInstance(): self
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle link.
|
||||
*
|
||||
* @return mixed|string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function link($type, $value)
|
||||
{
|
||||
if (empty($type) || empty($value) || ! in_array($type, self::TYPES)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
throw new \Exception('Value must be integer, string or object');
|
||||
}
|
||||
|
||||
if ($type == 'category') {
|
||||
if (! $value instanceof \Beike\Models\Category) {
|
||||
$value = \Beike\Models\Category::query()->find($value);
|
||||
}
|
||||
|
||||
return $value->url ?? '';
|
||||
} elseif ($type == 'product') {
|
||||
if (! $value instanceof \Beike\Models\Product) {
|
||||
$value = \Beike\Models\Product::query()->find($value);
|
||||
}
|
||||
|
||||
return $value->url ?? '';
|
||||
} elseif ($type == 'brand') {
|
||||
if (! $value instanceof \Beike\Models\Brand) {
|
||||
$value = \Beike\Models\Brand::query()->find($value);
|
||||
}
|
||||
|
||||
return $value->url ?? '';
|
||||
} elseif ($type == 'page') {
|
||||
if (! $value instanceof \Beike\Models\Page) {
|
||||
$value = \Beike\Models\Page::query()->find($value);
|
||||
}
|
||||
|
||||
return $value->url ?? '';
|
||||
} elseif ($type == 'page_category') {
|
||||
if (! $value instanceof \Beike\Models\PageCategory) {
|
||||
$value = \Beike\Models\PageCategory::query()->find($value);
|
||||
}
|
||||
|
||||
return $value->url ?? '';
|
||||
} elseif ($type == 'order') {
|
||||
return shop_route('account.order.show', ['number' => $value]);
|
||||
} elseif ($type == 'rma') {
|
||||
return shop_route('account.rma.show', ['id' => $value]);
|
||||
} elseif ($type == 'static') {
|
||||
return shop_route($value);
|
||||
} elseif ($type == 'custom') {
|
||||
if (Str::startsWith($value, ['http://', 'https://'])) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return "//{$value}";
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle link label
|
||||
*
|
||||
* @param $type
|
||||
* @param $value
|
||||
* @param $texts
|
||||
* @return mixed
|
||||
*/
|
||||
public function label($type, $value, $texts)
|
||||
{
|
||||
$types = ['category', 'product', 'brand', 'page', 'page_category', 'static', 'custom'];
|
||||
if (empty($type) || empty($value) || ! in_array($type, $types)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$locale = locale();
|
||||
$text = $texts[$locale] ?? '';
|
||||
if ($text) {
|
||||
return $text;
|
||||
}
|
||||
|
||||
if ($type == 'category') {
|
||||
return CategoryRepo::getName($value);
|
||||
} elseif ($type == 'product') {
|
||||
return ProductRepo::getName($value);
|
||||
} elseif ($type == 'brand') {
|
||||
return BrandRepo::getName($value);
|
||||
} elseif ($type == 'page') {
|
||||
return PageRepo::getName($value);
|
||||
} elseif ($type == 'page_category') {
|
||||
return PageCategoryRepo::getName($value);
|
||||
} elseif ($type == 'static') {
|
||||
return trans('shop/' . $value);
|
||||
} elseif ($type == 'custom') {
|
||||
return $text;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
|
@ -55,4 +55,12 @@ class Category extends Base
|
|||
{
|
||||
return $this->hasMany(ProductCategory::class);
|
||||
}
|
||||
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
$url = shop_route('categories.show', ['category' => $this]);
|
||||
$filters = hook_filter('model.category.url', ['url' => $url, 'category' => $this]);
|
||||
|
||||
return $filters['url'] ?? '';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue