generated from boboko/starter
homepage, new newsletter section, multilang support
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Lunar\Models\Product;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$products = Product::with(['variants.prices.currency', 'media'])
|
||||
->inRandomOrder()
|
||||
->limit(13)
|
||||
->get()
|
||||
->map(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]),
|
||||
]);
|
||||
|
||||
return view('home', [
|
||||
'heroProducts' => $products->take(5)->values(),
|
||||
'classicsProducts' => $products->slice(5)->values(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
class ProductController extends Controller
|
||||
{
|
||||
public function show(Product $product)
|
||||
public function show(string $locale, Product $product)
|
||||
{
|
||||
$product->load([
|
||||
"variants.prices.currency",
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?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