Cara menggunakan php getenv returns false

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

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
16 env var into an integer:

  • YAML
  • XML
  • PHP

1
2
3
4
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>

1
2
3
4
5
6
7
8
9
10
11
12
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};

Symfony provides the following env var processors:

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
17

Casts

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 to a string:

  • YAML
  • XML
  • PHP

1
2
3
4
5
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
0
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
1

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
19

Casts

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 to a bool (
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
21 values are
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
22,
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
23,
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
24 and all numbers except
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
25 and
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
26; everything else is
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
27):

  • YAML
  • XML
  • PHP

1
2
3
4
5
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
5

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
0
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
7

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
28

Casts

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 to a bool (just as
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
30 does) except it returns the inverted value (falsy values are returned as
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
21, truthy values are returned as
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
27):

  • YAML
  • XML
  • PHP

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
8
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
3

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
33Casts
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 to an int.
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
35Casts
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 to a float.
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
37

Finds the const value named in

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18:

  • YAML
  • XML
  • PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
7

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
9

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
39Decodes the content of
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18, which is a base64 encoded string.
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
41

Decodes the content of

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18, which is a JSON encoded string. It returns either an array or
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
43:

  • YAML
  • XML
  • PHP

1
2
3
4
5
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
3

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
0
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
5

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
44

If the content of

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 includes container parameters (with the syntax
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
46), it replaces the parameters by their values:

  • YAML
  • XML
  • PHP

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
6
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
9

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
6
1
2
3
4
5
6
7
8
9
10
11
12
1

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
47

Decodes the content of

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18, which is a CSV-encoded string:

  • YAML
  • XML
  • PHP

1
2
3
4
5
1
2
3
4
5
6
7
8
9
10
11
12
3

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
12
5

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
0
1
2
3
4
5
6
7
8
9
10
11
12
7

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
49

Randomly shuffles values of the

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 env var, which must be an array.

  • YAML
  • XML
  • PHP

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
6
1
2
3
4
5
6
7
8
9
10
11
12
9

// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
0
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
1

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <framework:config>
        <framework:router http-port="%env(int:HTTP_PORT)%"/>
    </framework:config>
</container>
6
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
3

6.2

The

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
51 env var processor was introduced in Symfony 6.2.

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
52

Returns the contents of a file whose path is the value of the

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 env var:

  • YAML
  • XML
  • PHP

1
2
3
4
5
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
7

1
2
3
4
5
// config/packages/framework.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework) {
    $framework->router()
        ->httpPort('%env(int:HTTP_PORT)%')
        // or
        ->httpPort(env('HTTP_PORT')->int())
    ;
};
9

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
54

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
55 the PHP file whose path is the value of the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 env var and return the value returned from it.

  • YAML
  • XML
  • PHP

1
2
3
4
5
1
2
3
4
5
1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
3

1
2
3
4
5
1
2
3
4
5
5

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
57

Trims the content of

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 env var, removing whitespaces from the beginning and end of the string. This is especially useful in combination with the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
59 processor, as it'll remove newlines at the end of a file.

  • YAML
  • XML
  • PHP

1
2
3
4
5
1
2
3
4
5
7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
9

1
2
3
4
5
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'
1

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
60

Retrieves the value associated with the key

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
18 from the array whose contents are stored in the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
62 env var:

  • YAML
  • XML
  • PHP

1
2
3
4
5
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'
5

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
8
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'
7

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
63

Retrieves the value of the parameter

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
64 when the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
62 env var is not available:

  • YAML
  • XML
  • PHP

1
2
3
4
5
# config/packages/framework.yaml
parameters:
    env(SECRET): 'some_secret'
framework:
    secret: '%env(string:SECRET)%'
9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1

1
2
3
4
5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3

When the fallback parameter is omitted (e.g.

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
66), then the returned value is
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
43.

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
68

Parses an absolute URL and returns its components as an associative array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
5

  • YAML
  • XML
  • PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
7

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
16
9

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
0
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
1

Caution

In order to ease extraction of the resource from the URL, the leading

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
69 is trimmed from the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
70 component.

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
71

Parses 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
14
2
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
3

  • YAML
  • XML
  • PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
4
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
6
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
7

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
8
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
9

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
72

Tries to convert an environment variable to an actual

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
73 value. This processor takes the fully qualified name of the
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
73 as an argument:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
4
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
01

  • YAML
  • XML
  • PHP

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
8
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
03

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
05

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
07

6.2

The

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
75 env var processor was introduced in Symfony 6.2.

It is also possible to combine any number of processors:

  • YAML
  • XML
  • PHP

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
8
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
09

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
0
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
11

<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:framework="http://symfony.com/schema/dic/symfony"
    xsi:schemaLocation="http://symfony.com/schema/dic/services
        https://symfony.com/schema/dic/services/services-1.0.xsd
        http://symfony.com/schema/dic/symfony
        https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

    <parameters>
        <parameter key="env(SECRET)">some_secret</parameter>
    </parameters>

    <framework:config secret="%env(string:SECRET)%"/>
</container>
8
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
13

It'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
14
6
# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
15

To enable the new processor in the app, register it as a service and tag it with the

# config/packages/framework.yaml
framework:
    router:
        http_port: '%env(int:HTTP_PORT)%'
76 tag. If you're using the , this is already done for you, thanks to .