|
9 | 9 |
|
10 | 10 | If you want to execute some logic in Shopware and trigger the execution over an HTTP request or need some special data from Shopware over the API, you can create custom API endpoints in your app that allow you to execute a script when a request to that endpoint is made.
|
11 | 11 |
|
| 12 | +## Manipulate HTTP-headers to API responses |
| 13 | + |
12 | 14 | ::: info
|
13 |
| -Note that custom endpoints with app scripts were introduced in Shopware 6.4.9.0 and are not supported in previous versions. |
| 15 | +Note that the `response` hook was added in v6.6.10.4 and is not available in earlier versions. |
14 | 16 | :::
|
15 | 17 |
|
| 18 | +There is a specific `response` script hook, that allows you to manipulate the HTTP-headers of the response via app scripts. |
| 19 | +This is especially useful to adjust the security headers to your needs. |
| 20 | + |
| 21 | +To add a custom header to every response, you can do the following: |
| 22 | + |
| 23 | +```twig |
| 24 | +// Resources/scripts/response/response.twig |
| 25 | +{% do hook.setHeader('X-Frame-Options', 'SAMEORIGIN') %} |
| 26 | +``` |
| 27 | + |
| 28 | +Additionally, you can check the current value of a given header and adjust it accordingly: |
| 29 | + |
| 30 | +```twig |
| 31 | +// Resources/scripts/response/response.twig |
| 32 | +{% if hook.getHeader('X-Frame-Options') == 'DENY' %} |
| 33 | + {% do hook.setHeader('X-Frame-Options', 'SAMEORIGIN') %} |
| 34 | +{% endif %} |
| 35 | +``` |
| 36 | + |
| 37 | +You also have access to the route name of the current request and to the route scopes to control the headers for specific routes: |
| 38 | + |
| 39 | +```twig |
| 40 | +// Resources/scripts/response/response.twig |
| 41 | +{% if hook.routeName == 'frontend.detail.page' and hook.isInRouteScope('store-api') %} |
| 42 | + {% do hook.setHeader('X-Frame-Options', 'SAMEORIGIN') %} |
| 43 | +{% endif %} |
| 44 | +``` |
| 45 | + |
| 46 | +The possible route scopes are `storefront`, `store-api`, `api` and `administration`. |
| 47 | + |
16 | 48 | ## Custom Endpoints
|
17 | 49 |
|
18 | 50 | There are specialized script-execution endpoints for the `api`, `store-api` and `storefront` scopes.
|
|
0 commit comments