<?php
namespace AppBundle\Controller\Shop;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Repository\ProductRepository;
class HomepageController extends ResourceController
{ /** * @var EngineInterface */
private $templatingEngine;
/**
* @param EngineInterface $templatingEngine
*/
public function __construct(EngineInterface $templatingEngine)
{
$this->templatingEngine = $templatingEngine;
}
/**
* @param Request $request
*
* @return Response
*/
public function indexAction(Request $request)
{
$productRepository = $this->container->get('sylius.repository.product');
$first = $productRepository->findByOnHand();
return $this->templatingEngine->renderResponse('@SyliusShop/Homepage/index.html.twig', array('first' => $first));
}
}
$productRepository = $this->get('sylius.repository.product');
<?php
namespace AppBundle\Repository;
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Core\Model\ChannelInterface;
class ProductRepository extends BaseProductRepository
{
/**
* {@inheritdoc}
*/
public function findLatestByChannelAndTaxonCode(ChannelInterface $channel, $code, $count)
{
return $this->createQueryBuilder('o')
->innerJoin('o.channels', 'channel')
->addOrderBy('o.createdAt', 'desc')
->andWhere('o.enabled = true')
->andWhere('channel = :channel')
->innerJoin('o.taxons', 'taxon')
->andWhere('taxon.code = :code')
->setParameter('channel', $channel)
->setParameter('code', $code)
->setMaxResults($count)
->getQuery()
->getResult()
;
}
}
my config:
sylius_product:
resources:
product:
classes:
repository: AppBundle\Repository\ProductRepository
my routing:
sylius_shop_homepage:
path: /en_US/
methods: [GET]
defaults:
_controller: sylius.controller.shop.homepage:indexAction
and service:
services:
sylius.controller.shop.homepage:
class: AppBundle\Controller\Shop\HomepageController
arguments: ['@templating']
And generally, that I did not do according to the documentation - nothing works. please, help!
path: /en_US/ # configure a new path that has all the needed variables
methods: [GET]
defaults:
_controller: sylius.controller.product:indexAction # you make a call on the Product Controller's index action
_sylius:
template: $template
repository:
method: findLatestByChannelAndTaxonCode # here use the new repository method
arguments:
- "expr:service('sylius.context.channel').getChannel()"
- $code
- $count