I have a service file containing following lines of code.
namespace App\Service;
/**
* Class MyService
* @package App\Service
*/
class MyService
{
/**
* @var string
*/
private $newTest;
/**
* MyService constructor.
* @param string $newTest
*/
public function __construct(string $newTest)
{
$this->newTest = $newTest;
}
}
I have defined it under service
section in services.yaml
file.
my_service:
class: App\Service\MyService
bind:
$pcValue: '%env(resolve:LATEST)%'
Now I am trying to retrieve this pcValue
at the controller,
$this->get('my_service');
It is returning me following error:
`Service "my_service" not found: even though it exists in the app's container, the container inside "App\Controller\WheelController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.`
The problem is that you are not using dependency injection correctly to take advantage of auto-wiring.
In order to have your service available on your controller you just need to add the type-hinted service in the desired action in your controller.
Check this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.