Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update documentation for creating custom resource controller #12328

Merged
merged 4 commits into from
Feb 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 18 additions & 35 deletions docs/customization/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ getting a list of recommended products from your external api.

namespace App\Controller;

use FOS\RestBundle\View\View;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
use Sylius\Component\Resource\ResourceActions;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -75,29 +74,24 @@ getting a list of recommended products from your external api.
$this->isGrantedOr403($configuration, ResourceActions::SHOW);
$product = $this->findOr404($configuration);

// some custom provider service to retrieve recommended products
$recommendationService = $this->get('app.provider.product');

$recommendedProducts = $recommendationService->getRecommendedProducts($product);

$this->eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $product);

$view = View::create($product);

if ($configuration->isHtmlRequest()) {
$view
->setTemplate($configuration->getTemplate(ResourceActions::SHOW . '.html'))
->setTemplateVar($this->metadata->getName())
->setData([
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $product,
'recommendedProducts' => $recommendedProducts,
$this->metadata->getName() => $product,
])
;
return $this->render($configuration->getTemplate(ResourceActions::SHOW . '.html'), [
'configuration' => $configuration,
'metadata' => $this->metadata,
'resource' => $product,
'recommendedProducts' => $recommendedProducts,
$this->metadata->getName() => $product,
]);
}

return $this->viewHandler->handle($configuration, $view);
return $this->createRestView($configuration, $product);
}
}

Expand All @@ -111,18 +105,7 @@ getting a list of recommended products from your external api.
classes:
controller: App\Controller\ProductController

**3.** The next thing you have to do is to override the ``sylius.repository.product`` service definition in the ``config/services.yaml``.

.. code-block:: yaml

# config/services.yaml
services:
app.provider.product:
class: App\Provider\ProductProvider
arguments: ['@sylius.repository.product']
public: true

**4.** Disable autowire for your controller in ``config/services.yaml``
**3.** Disable autowire for your controller in ``config/services.yaml``

.. code-block:: yaml

Expand Down Expand Up @@ -160,27 +143,27 @@ If you still need the methods of the original ``HomepageController``, then copy

namespace App\Controller\Shop;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class HomepageController
{
/** @var EngineInterface */
private $templatingEngine;
/** @var Environment */
private $twig;

public function __construct(EngineInterface $templatingEngine)
public function __construct(Environment $twig)
{
$this->templatingEngine = $templatingEngine;
$this->twig = $twig;
}

public function indexAction(): Response
{
return $this->templatingEngine->renderResponse('@SyliusShop/Homepage/index.html.twig');
return new Response($this->twig->render('@SyliusShop/Homepage/index.html.twig'));
}

public function customAction(): Response
{
return $this->templatingEngine->renderResponse('custom.html.twig');
return new Response($this->twig->render('custom.html.twig'));
}
}

Expand All @@ -192,7 +175,7 @@ If you still need the methods of the original ``HomepageController``, then copy
services:
sylius.controller.shop.homepage:
class: App\Controller\Shop\HomepageController
arguments: ['@templating']
arguments: ['@twig']
tags: ['controller.service_arguments']

.. tip::
Expand Down