You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Shopware 6中创建带自定义条件列表的后端模块及PHP控制器

在Shopware 6中实现自定义后端列表接口(类似Shopware 5的listAction)

完全可以在Shopware 6中创建自定义的PHP控制器来处理你的特定列表查询需求,不需要完全依赖默认的Repository Search API。下面是一步步的实现方案:

1. 创建自定义API控制器(PHP)

Shopware 6基于Symfony框架,所以自定义API控制器需要遵循Symfony的规范,同时适配Shopware的上下文和权限体系。

首先在你的插件目录下创建控制器文件,比如src/Controller/Api/SwagBundleController.php

<?php

namespace YourPluginNamespace\Controller\Api;

use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Routing\Annotation\RouteScope;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use YourPluginNamespace\Repository\SwagBundleRepository;

/**
 * @RouteScope(scopes={"api"})
 */
class SwagBundleController extends AbstractController
{
    private SwagBundleRepository $bundleRepository;

    public function __construct(SwagBundleRepository $bundleRepository)
    {
        $this->bundleRepository = $bundleRepository;
    }

    /**
     * @Route("/api/v{version}/_action/swag-bundle/custom-list", name="api.action.swag-bundle.custom-list", methods={"GET"})
     */
    public function customListAction(Context $context): JsonResponse
    {
        // 这里添加你的自定义查询条件,比如只获取启用的bundle
        $criteria = new Criteria();
        $criteria->addFilter(new EqualsFilter('active', true));
        // 可以添加更多过滤、排序、分页逻辑

        $bundles = $this->bundleRepository->search($criteria, $context);

        return new JsonResponse([
            'total' => $bundles->getTotal(),
            'data' => $bundles->getEntities()->getElements(),
        ]);
    }
}
  • 注意替换YourPluginNamespace为你实际的插件命名空间
  • @RouteScope(scopes={"api"})确保这个控制器属于API范围
  • 注入你的自定义Repository(如果是默认实体,也可以用Shopware自带的Repository)

2. 配置路由(可选,但推荐显式配置)

虽然注解路由在Shopware 6中生效,但你也可以在插件的Resources/config/routes.xml中添加路由配置,确保路由被正确加载:

<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">

    <import resource="../../Controller/Api/" type="annotation" />
</routes>

3. 前端调用自定义API

替换你原来的repository.search代码,改用Shopware的httpClient来调用自定义接口:

created() { 
  this.httpClient = Shopware.Service('httpClient');
  this.httpClient.get('/api/v3/_action/swag-bundle/custom-list', {
    headers: Shopware.Context.api.headers
  })
  .then((response) => {
    this.bundles = response.data.data;
    // 如果需要分页,可以用response.data.total处理
  })
  .catch((error) => {
    console.error('获取自定义列表失败:', error);
  });
}

4. 权限控制

别忘了给这个API端点添加权限,确保只有授权的后端用户可以访问:

  • 在控制器的customListAction方法上添加@IsGranted注解(需要导入Symfony\Component\Security\Http\Attribute\IsGranted
  • 或者在路由注解中添加access属性:@Route(..., access="your_plugin_permission")
  • 你需要在插件的src/Resources/config/permissions.xml中定义对应的权限

关键说明

  • 是否需要创建新API?:是的,当默认的Repository Search无法满足你的复杂业务条件时,自定义API端点是最佳方案,它能让你完全控制查询逻辑。
  • Shopware 6 vs Shopware 5:Shopware 5的listAction是基于Zend框架的后端控制器,而Shopware 6完全迁移到了Symfony生态,所以自定义控制器遵循Symfony的路由、依赖注入和安全规范,比Shopware 5更灵活。
  • 官方文档参考:你可以查看Shopware官方文档中的「Custom API endpoints」和「Backend Modules」章节,里面有更详细的规范和示例。

内容的提问来源于stack exchange,提问作者Abin John

火山引擎 最新活动