Symfony 7’de bir konsol komutu oluşturarak kullanıcı eklemek için aşağıdaki adımları takip edebilirsin:
1. Command Class Oluştur
Symfony’de bir komut oluşturmak için make:command
kullanabilirsin:
Shell
x
php bin/console make:command app:add-user
Bu komut, src/Command/AddUserCommand.php
dosyasını oluşturur. Bu dosyada, komutun mantığını yazacağız.
2. Command Class’ı Düzenle
Aşağıda örnek bir AddUserCommand
sınıfını görebilirsin:
PHP
xxxxxxxxxx
<?php
namespace App\Command;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class AddUserCommand extends Command
{
protected static $defaultName = 'app:add-user';
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct();
$this->entityManager = $entityManager;
}
protected function configure(): void
{
$this
->setDescription('Add a new user to the database')
->addOption('username', null, InputOption::VALUE_REQUIRED, 'The username of the new user')
->addOption('email', null, InputOption::VALUE_REQUIRED, 'The email of the new user')
->addOption('password', null, InputOption::VALUE_REQUIRED, 'The password of the new user');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$username = $input->getOption('username');
$email = $input->getOption('email');
$password = $input->getOption('password');
if (!$username || !$email || !$password) {
$io->error('All options (username, email, password) are required!');
return Command::FAILURE;
}
$user = new User();
$user->setUsername($username);
$user->setEmail($email);
$user->setPassword(password_hash($password, PASSWORD_BCRYPT)); // Şifreyi hashleyerek sakla
$this->entityManager->persist($user);
$this->entityManager->flush();
$io->success('User added successfully!');
return Command::SUCCESS;
}
}
3. Kullanıcı Varlığı (Entity) Kontrol Et
Yukarıdaki komutun çalışması için User
entity’si olması gerekir. Örnek bir User
entity’si:
PHP
xxxxxxxxxx
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private int $id;
#[ORM\Column(type: 'string', length: 255, unique: true)]
private string $username;
#[ORM\Column(type: 'string', length: 255, unique: true)]
private string $email;
#[ORM\Column(type: 'string', length: 255)]
private string $password;
public function getId(): int
{
return $this->id;
}
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
}
4. Command’ı Çalıştır
Komutu aşağıdaki gibi çalıştırabilirsin:
Python
xxxxxxxxxx
php bin/console app:add-user --username=johndoe --email=john@example.com --password=securepass123
Bu, kullanıcıyı veritabanına ekler.