The main issue of env vars is that their values can only be strings and your application may need other data types (integer, boolean, etc.). Symfony solves this problem with "env var processors", which transform the original contents of the given environment variables. The following example uses the integer processor to turn the value of the 16 env var into an integer:
1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 2 3 4 5 6 7 8 9 10 11 12
Symfony provides the following env var processors: 17Casts 18 to a string:
1 2 3 4 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
0 1 19Casts 18 to a bool ( 21 values are 22, 23, 24 and all numbers except 25 and 26; everything else is 27):
1 2 3 4 5 31 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 5 0 7 28Casts 18 to a bool (just as 30 does) except it returns the inverted value (falsy values are returned as 21, truthy values are returned as 27):
8 91 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10 11 12 13 141 1 2 3 4 5 6 7 8 9 10 11 12 13 142 1 2 3 4 5 6 7 8 9 10 11 12 13 143 33Casts 18 to an int. 35Casts 18 to a float. 37Finds the const value named in 18:
1 2 3 4 5 6 7 8 9 10 11 12 13 144 1 2 3 4 5 6 7 8 9 10 11 12 13 145 1 2 3 4 5 6 7 8 9 10 11 12 13 146 1 2 3 4 5 6 7 8 9 10 11 12 13 147 01 2 3 4 5 6 7 8 9 10 11 12 13 149 39Decodes the content of 18, which is a base64 encoded string. 41Decodes the content of 18, which is a JSON encoded string. It returns either an array or 43:
1 2 3 4 5 11 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 3 0 5 44If the content of 18 includes container parameters (with the syntax 46), it replaces the parameters by their values:
6 71 2 3 4 5 6 7 8 9 10 11 12 13 140 9 61 2 3 4 5 6 7 8 9 10 11 121 47Decodes the content of 18, which is a CSV-encoded string:
1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 123 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 5 6 7 8 9 10 11 125 01 2 3 4 5 6 7 8 9 10 11 127 49Randomly shuffles values of the 18 env var, which must be an array.
61 2 3 4 5 6 7 8 9 10 11 129 0 1 6 36.2 The 51 env var processor was introduced in Symfony 6.2. 52Returns the contents of a file whose path is the value of the 18 env var:
1 2 3 4 5 51 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 71 2 3 4 5 9 54 55 the PHP file whose path is the value of the 18 env var and return the value returned from it.
1 2 3 4 5 1 2 3 4 51 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 53 1 2 3 4 5 1 2 3 4 55 57Trims the content of 18 env var, removing whitespaces from the beginning and end of the string. This is especially useful in combination with the 59 processor, as it'll remove newlines at the end of a file.
1 2 3 4 5 1 2 3 4 57 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 2 3 4 59 1 2 3 4 5 1 60Retrieves the value associated with the key 18 from the array whose contents are stored in the 62 env var:
1 2 3 4 5 31 2 3 4 5 6 7 8 9 10 11 12 13 140 5 8 7 63Retrieves the value of the parameter 64 when the 62 env var is not available:
1 2 3 4 5 91 2 3 4 5 6 7 8 9 10 11 12 13 140 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 161 1 2 3 4 5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 163 When the fallback parameter is omitted (e.g. 66), then the returned value is 43. 68Parses an absolute URL and returns its components as an associative array. 1 2 3 4 5 6 7 8 9 10 11 12 13 142 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 165
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 166 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 167 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 169 0 1Caution In order to ease extraction of the resource from the URL, the leading 69 is trimmed from the 70 component. 71Parses the query string part of the given URL and returns its components as an associative array. 1 2 3 4 5 6 7 8 9 10 11 12 13 142 3
1 2 3 4 5 6 7 8 9 10 11 12 13 144 51 2 3 4 5 6 7 8 9 10 11 12 13 14 15 166 7 8 9 72Tries to convert an environment variable to an actual 73 value. This processor takes the fully qualified name of the 73 as an argument:1 2 3 4 5 6 7 8 9 10 11 12 13 144 01
8 031 2 3 4 5 6 7 8 9 10 11 12 13 14 051 2 3 4 5 6 7 8 9 10 11 12 13 142 076.2 The 75 env var processor was introduced in Symfony 6.2.It is also possible to combine any number of processors:
8 09 0 11 8 13It's also possible to add your own processors for environment variables. First, create a class that implements EnvVarProcessorInterface: 1 2 3 4 5 6 7 8 9 10 11 12 13 146 15To enable the new processor in the app, register it as a service and tag it with the 76 tag. If you're using the , this is already done for you, thanks to . |