780 B
780 B
API calls from the Front
To make an API call from the Front you've got to use native fetch function in a Stimulus controller.
Do not use hard-coded path in your Controller. Get the path from the back in your twig template and forward it to your Stimulus Controller with a Value attribute.
Example for the product component:
<!-- templates/components/item/_product.html.twig -->
data-product-route-value="{{ path('_api_/product/{id}/switchStatus_post', {id: product.id}) }}"
// assets/controllers/product_controller.js
const response = await fetch(this.routeValue, { method: 'POST' })