Symfony & Dependency injection #666
-
Hello, I am using a middleware ( // App/Telegram/Middleware/SaveTelegramUserMiddleware <?php
namespace App\Telegram\Middleware;
use App\Entity\TelegramUser;
use App\Repository\TelegramUserRepository;
use SergiX44\Nutgram\Nutgram;
class SaveTelegramUserMiddleware
{
protected TelegramUserRepository $telegramUserRepository;
public function __construct(TelegramUserRepository $telegramUserRepository)
{
$this->telegramUserRepository = $telegramUserRepository;
}
public function __invoke(Nutgram $bot, $next): void
{
// ...
$this->telegramUserRepository->updateOrCreateUser($user);
$next($bot);
}
} When I'm running my bot, which uses this middleware, in However, I am creating a bot object somewhere else in the code, like so: $bot = new Nutgram($token, $configuration);
// ...
$bot->middleware(SaveTelegramUserMiddleware::class); And every time I try using that bot using My question is: How do you use dependency injection properly with Nutgram? How can I use the service container with programmatically created bots? And most importantly: is this documented? Thank you! PS: Symfony 7.0.3 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, yes, there is an undocumented configuration: "container". Using Nutgram's container:$bot = new Nutgram('your-token');
$bot->getContainer()->set(CustomService::class, new CustomService()); Set a custom container:$config = new Configuration(container: $yourContainer);
$bot = new Nutgram('your-token', $config); This mode is done automatically by the frameworks: |
Beta Was this translation helpful? Give feedback.
Hi, yes, there is an undocumented configuration: "container".
This configuration is done automatically on Laravel and Symfony frameworks.
If you want to use an external service container, you have to set it in Nutgram's configuration or use its container.
Using Nutgram's container:
Set a custom container:
This mode is done automatically by the frameworks: