Miscellaneous script services reference
services.request (Shopware\Core\Framework\Routing\Facade\RequestFacade)
The request service allows you to access the current request in the script Examples:
{% block response %}
{% if services.request.method != "POST" %}
{% set response = services.response.json({
'error': 'Only POST requests are allowed',
}, 405) %}
{% do hook.setResponse(response) %}
{% return %}
{% endif %}
{% set response = services.response.json(services.request.request) %}
{% do hook.setResponse(response) %}
{% endblock %}ip()
The ip method returns the real client ip address
Returns
string|nullrequest client ip address
scheme()
The scheme method returns the request scheme
Returns
stringrequest scheme
method()
The method returns the request method in upper case
Returns
stringrequest method in upper case
uri()
The method
urireturns the request uri with the resolved urlReturns
stringrequest uri
pathInfo()
The method
pathInforeturns the request path info. The path info can be also an internal link when a seo url is used.Returns
stringrequest path info
query()
The method
queryreturns all query parameters as an arrayReturns
arrayrequest query parameters
request()
The method
requestreturns all post parameters as an array.On
application/jsonrequests this contains also the json body parsed as an array.Returns
arrayrequest post parameters
headers()
The method
headersreturns all request headers as an array.It is possible to access only the following headers: content-type, content-length, accept, accept-language, user-agent, referer
Returns
arrayrequest headers
cookies()
The method
cookiesreturns all request cookies as an array.Returns
arrayrequest cookies
Shopware\Core\Framework\Script\Facade\ArrayFacade
The ArrayFacade acts as a wrapper around an array and allows easier manipulation of arrays inside scripts. An array facade can also be accessed like a "normal" array inside twig. Examples:
{% do array.push('test') %}
{% do array.foo = 'bar' }
{% do array.has('foo') }
{% if array.foo === 'bar' %}
{% foreach array as key => value %}set()
set()adds a new element to the array using the given key.Arguments:
string|intkey: The array key.mixedvalue: The value that should be added.
Examples:
Add a new element with key
testand value 1.twig{% set product = services.cart.products.get(hook.ids.get('p1')) %} {% do product.payload.set('test', 1) %}
push()
push()adds a new value to the end of the array.Arguments:
mixedvalue: The value that should be added.
removeBy()
removeBy()removes the value at the given index from the array.Arguments:
string|intindex: The index that should be removed.
remove()
remove()removes the given value from the array. It does nothing if the provided value does not exist in the array.Arguments:
mixedvalue: The value that should be removed.
reset()
reset()removes all entries from the array.
merge()
merge()recursively merges the array with the given array.Arguments:
array<string|int,mixed>|\ArrayFacadearray: The array that should be merged with this array. Either a plainarrayor anotherArrayFacade.
Examples:
Merge two arrays.
twig{% set my_array = array({'bar': 'foo', 'baz': true}) %} {% do product.payload.merge(my_array) %}
replace()
replace()recursively replaces elements from the given array into this array.Arguments:
array<string|int,mixed>|\ArrayFacadearray: The array from which the elements should be replaced into this array. Either a plainarrayor anotherArrayFacade.
Examples:
Replace elements in the product payload array.
twig{% set second = array({'bar': 'baz'}) %} {% do product.payload.replace(second) %}
count()
count()returns the count of elements inside this array.Returns
intReturns the count of elements.
all()
all()function returns all elements of this array.Returns
arrayReturns all elements of this array.
services.config (Shopware\Core\System\SystemConfig\Facade\SystemConfigFacade)
The config service allows you to access the shop's and your app's configuration values.
get()
The
get()method allows you to access all config values of the store.Notice that your app needs the
system_config:readprivilege to use this method.Returns
array|bool|float|int|string|nullArguments:
stringkey: The key of the configuration value e.g.core.listing.defaultSorting.string|nullsalesChannelId: The SalesChannelId if you need the config value for a specific SalesChannel, if you don't provide a SalesChannelId, the one of the current Context is used as default.Default:
null
Examples:
Read an arbitrary system_config value.
twig{% set systemConfig = services.config.get('core.listing.productsPerPage') %}
app()
The
app()method allows you to access the config values your app's configuration.Notice that your app does not need any additional privileges to use this method, as you can only access your own app's configuration.
Returns
array|bool|float|int|string|nullArguments:
stringkey: The name of the configuration value specified in the config.xml e.g.exampleTextField.string|nullsalesChannelId: The SalesChannelId if you need the config value for a specific SalesChannel, if you don't provide a SalesChannelId, the one of the current Context is used as default.Default:
null
Examples:
Read your app's config value.
twig{% set appConfig = services.config.app('app_config') %}