もがき系プログラマの日常

もがき系エンジニアの勉強したこと、日常のこと、気になっている技術、備忘録などを紹介するブログです。

symfony5 始めた

はじめに

こんばんは。

副業で symfony5を触る機会がありました。

人生二回目なので、忘れないうちに備忘録残しときます。

試してみたことを羅列する感じになります。

本題

1. symfony server:start

symfonyコマンドでローカルでサーバを立ち上げます。

結構ぐぐると、symfony5では使えないから phpのビルトインサーバを使えという記事が多くありました。

ただ、最新版で実行すれば問題なく立ち上げられます。

2. symfony console make:controller home

コントローラと対応するテンプレートtwigを生成します。

$ symfony console make:controller home 

 created: src/Controller/HomeController.php
 created: templates/home/index.html.twig

           
  Success! 
           

 Next: Open your new controller class and add some pages!

3. symfony console make:user

ログインで使われる User Entityを生成します。

$ symfony console make:user               

 The name of the security user class (e.g. User) [User]:
 > 

 Do you want to store user data in the database (via Doctrine)? (yes/no) [yes]:
 > 

 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).

 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

4. symfony console make:migration

databaseからマイグレーションファイルを生成します。

$ symfony console make:migration 


           
  Success! 
           

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

5. symfony console doctrine:migrations:migrate

作成したマイグレーションを実行します。

$ symfony console doctrine:migrations:migrate

[notice] Migrating up to DoctrineMigrations\Version20210514141922
[notice] finished in 89.4ms, used 20M memory, 1 migrations executed, 1 sql queries

6. symfony console make:auth

認証用のクラス諸々を生成します。

$ symfony console make:auth

 What style of authentication do you want? [Empty authenticator]:
  [0] Empty authenticator
  [1] Login form authenticator
 > 1

 The class name of the authenticator to create (e.g. AppCustomAuthenticator):
 >    

 Choose a name for the controller class (e.g. SecurityController) [SecurityController]:
 > 

 Do you want to generate a '/logout' URL? (yes/no) [yes]:
 > 

 created: src/Security/AppCustomAuthenticator.php
 updated: config/packages/security.yaml
 created: src/Controller/SecurityController.php
 created: templates/security/login.html.twig

           
  Success! 
           

 Next:
 - Customize your new authenticator.
 - Finish the redirect "TODO" in the App\Security\AppCustomAuthenticator::onAuthenticationSuccess() method.
 - Review & adapt the login template: templates/security/login.html.twig.

f:id:kojirooooocks:20210515031954p:plain

いい感じでできてきました。

終わりに

これからも開発続くので、第2弾もありそう。

現場からは以上です。