LoginSignup
1
1

More than 1 year has passed since last update.

【Symfony】0からユーザー登録機能を作るまで

Posted at

概要

composer require --dev symfony/maker-bundle
このコマンドを打つだけでたくさんの機能が使えるようになったので、試しにゼロからユーザー登録機能を追加してみようと思います。
上記のコマンドの説明はこの記事
https://qiita.com/yuki8634/items/7af65946acefbc3a471b

コマンド

% bin/console make:user

# ユーザークラスの名前を指定できます(指定しない場合は勝手にUserになる)
 The name of the security user class (e.g. User) [User]:
 > 

# ユーザーデータをデータベースに保存しますか?(そのままEnter押すとYesになる)
 Do you want to store user data in the database (via Doctrine)? (yes/no) [yes]:
 > 

# Unique(他のユーザーと被らない)なプロパティの名前を指定する(指定しない場合email)
 Enter a property name that will be the unique "display" name for the user (e.g. email, username, uuid) [email]:
 > 

 Will this app need to hash/check user passwords? Choose No if passwords are not needed or will be checked/hashed by some other system (e.g. a single sign-on server).

# ハッシュしたユーザーのパスワードが必要ですか?(そのままEnter押すとYesになる)
 Does this app need to hash/check user passwords? (yes/no) [yes]:
 > 

 created: src/Entity/User.php
 created: src/Repository/UserRepository.php
 updated: src/Entity/User.php
 updated: config/packages/security.yaml

           
  Success! 
           

 Next Steps:
   - Review your new App\Entity\User class.
   - Use make:entity to add more fields to your User entity and then run make:migration.
   - Create a way to authenticate! See https://symfony.com/doc/current/security.html
% bin/console make:migration

 Success! 
           

 Next: Review the new migration "migrations/Version20230330142240.php"
 Then: Run the migration with php bin/console doctrine:migrations:migrate
 See https://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html
% bin/console doctrine:migrations:migrate

# 注意!このまま進むことによってデータベースのデータが変更されたり、失われてしまう可能性があります。それでも進みますか?
 WARNING! You are about to execute a migration in database "hello-symfony4" that could result in schema changes and data loss. Are you sure you wish to continue? (yes/no) [yes]:
 > 

[notice] Migrating up to DoctrineMigrations\Version20230330142240
[notice] finished in 72.9ms, used 20M memory, 1 migrations executed, 1 sql queries
                                                                                
 [OK] Successfully migrated to version :                                        
      DoctrineMigrations\Version20230330142240                               
% bin/console make:registration-form

 Creating a registration form for App\Entity\User

# 重複したユーザーが作成されることを避けるためにユーザークラスに@UniqueEntityバリデーションをつけますか?
 Do you want to add a @UniqueEntity validation annotation on your User class to make sure duplicate accounts aren't created? (yes/no) [yes]:
 > 

# アカウント登録後メールアドレスを確認するためにメールを送信しますか?
 Do you want to send an email to verify the user's email address after registration? (yes/no) [yes]:
 > 

                                                                                # 注意!コンポーネントが足りないので、この操作が終わったら下記のコマンドを実行してくださいね
 [WARNING] We're missing some important components. Don't forget to install     
           these after you're finished.                                         
           composer require symfonycasts/verify-email-bundle                 

 By default, users are required to be authenticated when they click the verification link that is emailed to them.
 This prevents the user from registering on their laptop, then clicking the link on their phone, without
 having to log in. To allow multi device email verification, we can embed a user id in the verification link.

# 匿名メールの確認を許可するためにユーザーIDをリンクに含ませますか?
 Would you like to include the user id in the verification link to allow anonymous email verification? (yes/no) [no]:
 > 

# どのメールアドレスを使って確認メールを送信しますか?
 What email address will be used to send registration confirmations? (e.g. mailer@your-domain.com):
 > 

# 送信元の名前はどうしますか?
 What "name" should be associated with that email address? (e.g. Acme Mail Bot):
 > 

# ユーザー登録後自動的にユーザーに権限を与えますか?
 Do you want to automatically authenticate the user after registration? (yes/no) [yes]:
 > 

 ! [NOTE] No Guard authenticators found - so your user won't be automatically   
 !        authenticated after registering.                                      

# ユーザー登録後どの画面にリダイレクトしますか?
 What route should the user be redirected to after registration?:
  [0 ] _preview_error
  [1 ] _wdt
  [2 ] _profiler_home
  [3 ] _profiler_search
  [4 ] _profiler_search_bar
  [5 ] _profiler_phpinfo
  [6 ] _profiler_xdebug
  [7 ] _profiler_search_results
  [8 ] _profiler_open_file
  [9 ] _profiler
  [10] _profiler_router
  [11] _profiler_exception
  [12] _profiler_exception_css
  [13] app_form
  [14] app_top
 > 14

 updated: src/Entity/User.php
 updated: src/Entity/User.php
 created: src/Security/EmailVerifier.php
 created: templates/registration/confirmation_email.html.twig
 created: src/Form/RegistrationFormType.php
 created: src/Controller/RegistrationController.php
 created: templates/registration/register.html.twig

           
  Success! 
           

 Next:
 1) Install some missing packages:
      composer require symfonycasts/verify-email-bundle
 2) In RegistrationController::verifyUserEmail():
    * Customize the last redirectToRoute() after a successful email verification.
    * Make sure you're rendering success flash messages or change the $this->addFlash() line.
 3) Review and customize the form, controller, and templates as needed.
 4) Run "php bin/console make:migration" to generate a migration for the newly added User::isVerified property.

 Then open your browser, go to "/register" and enjoy your new form!
% composer require symfonycasts/verify-email-bundle
% bin/console make:migration
% bin/console doctrine:migrations:migrate

結果

こんな感じの画面になります
image.png
eccde3f1abe982320d43ddb2e7b23f8a.png

感想

次何をすればいいのかを明確に書いてくれるので、

間違えにくく確実に実装できる 所が良いポイントだと思います。

まだメール送信の確認ができてないので、また新たにわかったことがあれば

この記事の編集か、新しい記事出します。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1