在 Sylius 的 ResourceController 中创建自定义操作
Create custom action in ResourceController in Sylius
我有一个扩展 ResourceController
的自定义控制器,我正在尝试创建一个自定义操作 public function redeemAction(Request $request): Response
namespace App\Controller;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
class OrderPointsController extends ResourceController
{
public function redeemAction(Request $request): Response {
}
}
这是我在 sylius_shop.yaml
的路线
app_shop_account_redeem_points:
path: /account/points/redeem
methods: [POST]
defaults:
_controller: app.controller.order_points:redeemAction
和 resource.yaml:
sylius_resource:
resources:
app.order_points:
classes:
model: App\Entity\Order\OrderPoints
repository: App\Repository\OrderPointsRepository
当我通过 POST 访问路由时,出现此错误:
The controller for URI "/account/points/redeem" is not callable:
Expected method "redeemAction" on class
"Sylius\Bundle\ResourceBundle\Controller\ResourceController", did you
mean "createAction", "deleteAction", "indexAction"?
我知道资源控制器应该执行 CRUD 操作,但一些核心 Sylius 文件也调用自定义操作:
sylius_shop_cart_summary:
path: /
methods: [GET]
defaults:
_controller: sylius.controller.order:summaryAction
_sylius:
template: "@SyliusShop/Cart/summary.html.twig"
form: Sylius\Bundle\OrderBundle\Form\Type\CartType
.
namespace Sylius\Bundle\OrderBundle\Controller;
class OrderController extends ResourceController
{
public function summaryAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$cart = $this->getCurrentCart();
if (null !== $cart->getId()) {
$cart = $this->getOrderRepository()->findCartById($cart->getId());
}
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($cart));
}
$form = $this->resourceFormFactory->create($configuration, $cart);
$view = View::create()
->setTemplate($configuration->getTemplate('summary.html'))
->setData([
'cart' => $cart,
'form' => $form->createView(),
])
;
return $this->viewHandler->handle($configuration, $view);
}
...
}
有没有办法在扩展资源控制器的自定义控制器中调用除预定义 CRUD 操作之外的其他操作?
我在 resource.yaml 中缺少控制器:
sylius_resource:
resources:
app.order_points:
classes:
model: App\Entity\Order\OrderPoints
repository: App\Repository\OrderPointsRepository
controller: App\Controller\OrderPointsController
我有一个扩展 ResourceController
的自定义控制器,我正在尝试创建一个自定义操作 public function redeemAction(Request $request): Response
namespace App\Controller;
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
class OrderPointsController extends ResourceController
{
public function redeemAction(Request $request): Response {
}
}
这是我在 sylius_shop.yaml
的路线app_shop_account_redeem_points:
path: /account/points/redeem
methods: [POST]
defaults:
_controller: app.controller.order_points:redeemAction
和 resource.yaml:
sylius_resource:
resources:
app.order_points:
classes:
model: App\Entity\Order\OrderPoints
repository: App\Repository\OrderPointsRepository
当我通过 POST 访问路由时,出现此错误:
The controller for URI "/account/points/redeem" is not callable: Expected method "redeemAction" on class "Sylius\Bundle\ResourceBundle\Controller\ResourceController", did you mean "createAction", "deleteAction", "indexAction"?
我知道资源控制器应该执行 CRUD 操作,但一些核心 Sylius 文件也调用自定义操作:
sylius_shop_cart_summary:
path: /
methods: [GET]
defaults:
_controller: sylius.controller.order:summaryAction
_sylius:
template: "@SyliusShop/Cart/summary.html.twig"
form: Sylius\Bundle\OrderBundle\Form\Type\CartType
.
namespace Sylius\Bundle\OrderBundle\Controller;
class OrderController extends ResourceController
{
public function summaryAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$cart = $this->getCurrentCart();
if (null !== $cart->getId()) {
$cart = $this->getOrderRepository()->findCartById($cart->getId());
}
if (!$configuration->isHtmlRequest()) {
return $this->viewHandler->handle($configuration, View::create($cart));
}
$form = $this->resourceFormFactory->create($configuration, $cart);
$view = View::create()
->setTemplate($configuration->getTemplate('summary.html'))
->setData([
'cart' => $cart,
'form' => $form->createView(),
])
;
return $this->viewHandler->handle($configuration, $view);
}
...
}
有没有办法在扩展资源控制器的自定义控制器中调用除预定义 CRUD 操作之外的其他操作?
我在 resource.yaml 中缺少控制器:
sylius_resource:
resources:
app.order_points:
classes:
model: App\Entity\Order\OrderPoints
repository: App\Repository\OrderPointsRepository
controller: App\Controller\OrderPointsController