Shopware 6后端控制器路径

时间:2020-08-02 11:02:03

标签: symfony shopware shopware6

在shopware 6中,我想从后端/管理页面(javascript)调用后端/管理API控制器。可能是通过内置的getter函数使用相对路径的正确方法是什么?

仅当商店位于/api/v1上时,提取/才有效,而当它位于子文件夹中时,则不可用。

fetch('/api/v1/my-plugin/my-custom-action', ...)

2 个答案:

答案 0 :(得分:1)

最佳做法是编写自己的JS服务,以处理与api端点的通信。

我们有一个抽象的import UIKit import RxSwift import RxCocoa class HomeViewController: UIViewController { private let disposeBag = DisposeBag() var viewModel = HomeViewModel() override func viewDidLoad() { super.viewDidLoad() let viewDidAppear = rx.sentMessage(#selector(UIViewController.viewDidAppear(_:))) .mapToVoid() .asDriverOnErrorJustComplete() // How to merge viewWillAppear & alert for callback of retry button? let input = HomeViewModel.Input(syncData: viewDidAppear) let output = viewModel.transform(input: input) output.message.drive(alert) .disposed(by: disposeBag) } var alert: Binder<String> { return Binder(self) { (vc, message) in let alert = UIAlertController(title: "Sync failed!", message: message, preferredStyle: .alert) let okay = UIAlertAction(title: "Retry", style: .default, handler: { _ in // how to call syncData of Input? }) let dismiss = UIAlertAction(title: "Dismiss", style: UIAlertAction.Style.cancel, handler: nil) alert.addAction(okay) alert.addAction(dismiss) vc.present(alert, animated: true, completion: nil) } } } 类,您可以从中继承。您可以在the platform中查看ApiService的示例。 对您来说,实现可能看起来像这样:

CalculatePriceApiService

请注意,您的api服务已预先配置为在构造函数的第一行中与您的class MyPluginApiService extends ApiService { constructor(httpClient, loginService, apiEndpoint = 'my-plugin') { super(httpClient, loginService, apiEndpoint); this.name = 'myPluginService'; } myCustomAction() { return this.httpClient .get('my-custom-action', { headers: this.getBasicHeaders() }) .then((response) => { return ApiService.handleResponse(response); }); } } 端点通信,这意味着在以下所有请求中,您都可以使用相对路由路径。

还请记住,抽象的ApiService将负责解决用于请求的配置。特别是,这意味着ApiService将使用正确的BaseDomain(包括子文件夹),并且它将自动使用商店软件版本支持的apiVersion。这意味着每次有新的api版本可用时,ApiService在路由中使用的apiVersion都会增加,这意味着您需要在api版本的后端路由注释中使用通配符。

最后请记住,您需要注册该服务。那就是documented here。 对您来说,这可能像这样:

my-plugin

答案 1 :(得分:0)

如果要讨论实现的自定义操作,则需要定义路由(使用批注)并在Resources \ config \ routes.xml中的route.xml中注册控制器。

请遵循该文档 https://docs.shopware.com/en/shopware-platform-dev-en/how-to/api-controller