generated from boboko/starter
localization from boboko core and product service too
This commit is contained in:
@@ -2,24 +2,46 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Lunar\Models\Collection;
|
||||
use Lunar\Models\Product;
|
||||
use Modules\Core\Catalog\ProductFilters;
|
||||
use Modules\Core\Catalog\ProductService;
|
||||
|
||||
class CategoryController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProductService $products,
|
||||
) {}
|
||||
|
||||
public function show(string $locale, Collection $collection)
|
||||
{
|
||||
$products = $collection
|
||||
->products()
|
||||
->with(['variants.prices.currency', 'media'])
|
||||
->paginate(12)
|
||||
->withQueryString()
|
||||
->through(fn (Product $product) => [
|
||||
'name' => $product->translateAttribute('name'),
|
||||
'price' => $product->variants->first()?->prices->first()?->price->decimal,
|
||||
'image' => $product->media->first()?->getUrl(),
|
||||
'href' => route('product.show', ['product' => $product]),
|
||||
]);
|
||||
$perPage = 12;
|
||||
$page = (int) request('page', 1);
|
||||
|
||||
// Listing/filtering reads from the Meilisearch index via ProductService,
|
||||
// not Eloquent — see Modules\Core\Catalog\ProductService. It returns plain
|
||||
// arrays (already localized/flattened), not Product models.
|
||||
$result = $this->products->list(
|
||||
filters: new ProductFilters(collectionId: $collection->id),
|
||||
perPage: $perPage,
|
||||
page: $page,
|
||||
);
|
||||
|
||||
$products = new LengthAwarePaginator(
|
||||
items: collect($result['data'])->map(fn (array $product) => [
|
||||
'name' => $product['name'],
|
||||
'price' => $product['price'],
|
||||
'image' => $product['media'][0]['url'] ?? null,
|
||||
'href' => route('product.show', ['product' => $product['id']]),
|
||||
]),
|
||||
total: $result['meta']['total'],
|
||||
perPage: $result['meta']['per_page'],
|
||||
currentPage: $result['meta']['current_page'],
|
||||
options: [
|
||||
'path' => request()->url(),
|
||||
'query' => request()->query(),
|
||||
],
|
||||
);
|
||||
|
||||
return view('category.show', [
|
||||
'collection' => $collection,
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class SetLocale
|
||||
{
|
||||
/**
|
||||
* Validate the {locale} route segment, apply it app-wide, and share
|
||||
* the current/alternate locale (and the alternate's URL) with all views
|
||||
* so the header switcher and layout hreflang tags don't recompute it.
|
||||
*/
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
$locale = $request->route('locale');
|
||||
$available = config('app.available_locales');
|
||||
|
||||
if (! in_array($locale, $available, true)) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
App::setLocale($locale);
|
||||
|
||||
// Lets route() calls omit {locale} anywhere in the request lifecycle
|
||||
// (controllers, views) — without this, every route() call inside the
|
||||
// {locale} group would need locale passed explicitly every time.
|
||||
URL::defaults(['locale' => $locale]);
|
||||
|
||||
$altLocale = collect($available)->first(fn ($l) => $l !== $locale);
|
||||
$routeName = $request->route()->getName();
|
||||
|
||||
View::share('currentLocale', $locale);
|
||||
View::share('altLocale', $altLocale);
|
||||
View::share(
|
||||
'altLocaleUrl',
|
||||
$routeName
|
||||
? route($routeName, array_merge($request->route()->parameters(), ['locale' => $altLocale]))
|
||||
: url('/'.$altLocale),
|
||||
);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user