File: D:/HostingSpaces/SBogers10/csb.komma.pro/app/Products/ProductController.php
<?php
namespace App\Products;
use App\Base\Controller;
use App\Components\ComponentService;
use App\Products\Models\Product;
use App\Projects\Projectservice;
final class ProductController extends Controller
{
private $productService;
private $projectService;
private $productPaginationKey = 'productPagination';
public function __construct(ProductService $productService, Projectservice $projectService)
{
parent::__construct();
$this->productService = $productService;
$this->projectService = $projectService;
}
/**
* @return \Illuminate\Contracts\View\View
*/
public function index()
{
$page = $this->links->products->node;
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($page->translation);
$products = $this->productService->getProductsPaginated();
$products->withPath($this->links->products->route);
// Filter out images without the default key (so no review images)
foreach($products as $prod){
$prod->documents = $prod->documents->filter(function($img, $i){
return ($img->key == 'Documents-products');
});
}
$heroImages = $page->documents->filter(function($img, $i){
return ($img->key == 'Documents-hero');
});
$this->keepTrackOfPagination($this->productPaginationKey);
// Make language menu for given page
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->{$page->code_name}, $this->links->home);
// Return view
return view('templates.products_index',[
'page' => $page,
'components' => $components,
'links' => $this->links,
'products' => $products,
]);
}
/**
* @param Product $product
* @return \Illuminate\Contracts\View\View
*/
public function show(Product $product)
{
$product->load('translation','translations','site','heroImages');
$componentService = app(ComponentService::class);
$components = $componentService->getViewComponents($product->translation);
// Create previous route for better navigation UX
$previousRoute = $this->createPreviousRoute($this->productPaginationKey, $this->links->products->route);
// Make language menu for given page
$page = $this->links->products->node;
$languageMenu = $this->pageService->makeLanguageSwitchForPage($this->links->products, $this->links->home);
// Return view
return view('templates.products_show',[
'page' => $page,
'components' => $components,
'product' => $product,
'links' => $this->links,
'languageMenu' => $languageMenu,
'previousRoute' => $previousRoute,
]);
}
}