Skip to content

Commit 4452055

Browse files
keulinhoIsengo1989
andauthored
docs: Add docs for response headers in app scripts (#1736)
* docs: Add docs for response headers in app scripts * Fix lint and spelling issues * chore: fix spellcheck * Adjust release version --------- Co-authored-by: Micha <[email protected]>
1 parent f692859 commit 4452055

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

.wordlist.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,10 @@ rfc
13691369
roadmap
13701370
Roadmap
13711371
rollbacking
1372+
routeName
13721373
RouteResponse
13731374
routeScope
1375+
routeScopes
13741376
RuleConditionService
13751377
runtime
13761378
RuntimeException

guides/plugins/apps/app-scripts/custom-endpoints.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,42 @@ nav:
99

1010
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.
1111

12+
## Manipulate HTTP-headers to API responses
13+
1214
::: 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.
1416
:::
1517

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+
1648
## Custom Endpoints
1749

1850
There are specialized script-execution endpoints for the `api`, `store-api` and `storefront` scopes.

resources/references/app-reference/script-reference/script-hooks-reference.md

+12
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,18 @@ All available hooks within the Store-API and API
524524
| **Available Services** | [repository](./data-loading-script-services-reference#RepositoryFacade)<br>[writer](./custom-endpoint-script-services-reference#RepositoryWriterFacade)<br>[config](./miscellaneous-script-services-reference#SystemConfigFacade)<br>[response](./custom-endpoint-script-services-reference#ScriptResponseFactoryFacade)<br> |
525525
| **Stoppable** | `true` |
526526

527+
#### response
528+
529+
| <!-- --> | <!-- --> |
530+
|:-----------------------|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
531+
| **Name** | response |
532+
| **Since** | 6.6.10.4 |
533+
| **Class** | `Shopware\Core\Framework\Script\Api\ResponseHook` |
534+
| **Description** | Triggered on every response<br> |
535+
| **Available Data** | routeName: `string`<br>routeScopes: `array`<br>context: [`Shopware\Core\Framework\Context`](https://github.com/shopware/shopware/blob/trunk/src/Core/Framework/Context.php)<br> |
536+
| **Available Services** | |
537+
| **Stoppable** | `false` |
538+
527539
### store-api-{hook}
528540

529541
**Interface Hook**

0 commit comments

Comments
 (0)