<?php declare(strict_types=1);
namespace H1web\Blog\Storefront\Controller;
use Shopware\Storefront\Controller\StorefrontController;
use Shopware\Core\Content\Cms\Exception\PageNotFoundException;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Framework\Cache\Annotation\HttpCache;
use H1web\Blog\Storefront\Page\Blog\BlogPageLoader;
use H1web\Blog\Storefront\Page\Tag\TagPageLoader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @RouteScope(scopes={"storefront"})
*/
class BlogController extends StorefrontController
{
/**
* @var BlogPageLoader
*/
private $blogPageLoader;
/**
* @var TagPageLoader
*/
private $tagPageLoader;
public function __construct(
BlogPageLoader $blogPageLoader,
TagPageLoader $tagPageLoader
) {
$this->blogPageLoader = $blogPageLoader;
$this->tagPageLoader = $tagPageLoader;
}
/**
* Route for a tag overview page
*
* @HttpCache()
* @Route("/h1webblog/tag/{tagId}", name="frontend.h1webblog.tag_overview", methods={"GET"})
*
* @throws MissingRequestParameterException
* @throws PageNotFoundException
*/
public function renderTagOverviewPage(string $tagId, Request $request, SalesChannelContext $salesChannelContext): Response
{
if (!$tagId) {
throw new MissingRequestParameterException('Parameter tagId missing');
}
$tagPage = $this->tagPageLoader->load($tagId, $request, $salesChannelContext);
return $this->renderStorefront(
'@Storefront/storefront/page/h1web_blog/tag_overview.html.twig',
[
'page' => $tagPage
]
);
}
/**
* Route for a single blog page
*
* @HttpCache()
* @Route("/h1webblog/{id}", name="frontend.h1webblog.blog", methods={"GET"})
*
* @throws MissingRequestParameterException
* @throws PageNotFoundException
*/
public function renderSinglePage(string $id, Request $request, SalesChannelContext $salesChannelContext): Response
{
if (!$id) {
throw new MissingRequestParameterException('Parameter id missing');
}
$blogPage = $this->blogPageLoader->load($id, $request, $salesChannelContext);
if (!$blogPage->getCmsPage()) {
return $this->renderStorefront('@Storefront/storefront/page/h1web_blog/index.html.twig', ['page' => $blogPage]);
}
return $this->renderStorefront('@Storefront/storefront/page/content/blog-detail.html.twig', ['page' => $blogPage]);
}
}