I am using The DependencyInjection Component of Symfony 4 and I am trying to define the configuration settings (in config/services.yml
) for creating a simple ServerRequest
object. ServerRequest
receives the $_SERVER
variable (array) as argument for the constructor parameter $serverParams
.
It should print the array, but, instead, it prints the string "$_SERVER":
Hello from ServerRequest!
...path-to-project-root/mytests/ServerRequest.php:14:string '$_SERVER' (length=8)
Maybe you have an idea? Thank you very much!
config/services.yml:
parameters:
services:
_defaults:
autowire: false
autoconfigure: true
public: true
MyTests\ServerRequest:
arguments:
$serverParams: $_SERVER
mytests/ServerRequest.php
<?php
namespace MyTests;
class ServerRequest {
private $serverParams;
public function __construct($serverParams) {
$this->serverParams = $serverParams;
echo 'Hello from ServerRequest!<br/>';
var_dump($this->serverParams);
}
}
bootstrap.php
<?php
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/*
* Include the Composer autoloader.
*/
require __DIR__ . '/vendor/autoload.php';
/*
* Create dependency injection container.
*/
$container = new ContainerBuilder();
$fileLocator = new FileLocator(__DIR__ . '/config');
$loader = new YamlFileLoader($container, $fileLocator);
$loader->load('services.yml');
$container->compile();
/*
* Create ServerRequest instance.
*/
$serverRequest = $container->get(\MyTests\ServerRequest::class);
composer.json
{
"require": {
"php": ">=5.5.0",
"symfony/dependency-injection": "^4.0",
"symfony/config": "^4.0",
"symfony/yaml": "^4.0"
},
"require-dev": {
"symfony/console": "^4.0",
"symfony/event-dispatcher": "^4.0",
"symfony/process": "^4.0",
"symfony/lock": "^4.0",
"symfony/debug": "^4.0",
"symfony/var-dumper": "^4.0"
},
"autoload": {
"psr-4": {
"MyTests\\": "mytests/"
}
}
}