Slide 1

Slide 1 text

【Symfony超入門】
 コマンドだけでCRUD画面を作るチート法
 志賀 彩乃


Slide 2

Slide 2 text

このスライドのゴール(私のもくろみ) SymfonyはLaravelと比べ、
 難しそうで疎遠されがち(個人の感想です)
 
 ↓
 
 「そんなことない」ってことを知ってもらう💡
 
 ↓
 
 Symfony信者を増やして、情報交換がしたいです🙇🙇🙇


Slide 3

Slide 3 text

Symfony CLIの必要はありません🙌
 本音は便利なのでSymfony CLIをインストールしてほしいですが、 
 「早い!うまい!やすい! 🍚」をモットーにcomposerとphpが実行できればokな方法で記述します。 
 $ composer --version Composer version 2.5.1 2022-12-22 15:33:54 $ php --version PHP 8.1.0 (cli) (built: Oct 31 2022 21:55:41) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.0, Copyright (c) Zend Technologies with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

Slide 4

Slide 4 text

スタート!


Slide 5

Slide 5 text

準備(6コマンドでおわるのでお付き合いください🙇)
 $ composer create-project symfony/skeleton qwitter --no-interaction $ cd qwitter $ php -S localhost:8000 -t public/ $ composer require security twig orm form validator annotations $ cp .env .env.local && vi .env.local $ php bin/console doctrine:database:create

Slide 6

Slide 6 text

composer create-project symfony/skeleton qwitter --no-interaction (symfony new --dir=qwitter でやってることとほぼ同じです ) 中身が特にないsymfonyプロジェクト(今回はプロジェクト名:qwitter)を作ります
 💡symfony newを使うとgit initしてくれたり.php-versionの作成もしてくれます。


Slide 7

Slide 7 text

php -S localhost:8000 -t public/ (symfony server:start がやってることとほぼ同じはず) 一旦、webサイトをローカルで確認してみましょう!
 → welcom to Symfony 6.2.7の画面 で歓迎してくれるのでやる気が出ます󰵗🫶
 
 http://localhost:8000/


Slide 8

Slide 8 text

composer require security twig orm form validator annotations (とりあえず、必要なやーつをおまじないでだーっといれます 🐹)


Slide 9

Slide 9 text

cp .env .env.local && vi .env.local 直前のコマンドでDB設定についてやるべきことを教えてくれます。
 これにそって.env.localのDATEBASE_URLを自身のローカルに合わせて設定してください 。
 Please review, edit and commit them: these files are yours. doctrine/doctrine-bundle instructions: * Modify your DATABASE_URL config in .env * Configure the driver (postgresql) and server_version (15) in config/packages/doctrine.yaml 💡新規作成したいデータベース名 
 を指定してください


Slide 10

Slide 10 text

php bin/console doctrine:database:create 直前で設定したDATEBASE_URLのデータベースを作成してくれます。
 よって、わざわざポスグレやMySQLに入ってcreate database db_nameする必要はありませ ん!

Slide 11

Slide 11 text

php bin/console Console Commandという名称で、開発していく上で超便利なコマンドをSymfonyがたくさん提供 してくれています。
 そのコマンドリストをbin/console listで確認できます。
 $ bin/console list … Available commands: about Display information about the current project completion Dump the shell completion script help Display help for a command list List commands assets assets:install Install bundle's web assets under a public directory … Laravelでいう php artisan みたいなやつだと思ってます(by Laravelにわか)


Slide 12

Slide 12 text

composer require --dev symfony/profiler-pack【任意】 デバッグに便利です!


Slide 13

Slide 13 text

本題に入ります!


Slide 14

Slide 14 text

composer require --dev symfony/maker-bundle ControllerやCommand、Entityを作ってくれるコマンドです!
 本日のメインです。
 Laravelでいうphp artisan make みたいなやつだと思ってます(by Laravelにわか)


Slide 15

Slide 15 text

makerBundleにより追加されたConsole Commandはこちら
 $ bin/console list ... make make:auth Creates a Guard authenticator of different flavors make:command Creates a new console command class make:controller Creates a new controller class make:crud Creates CRUD for Doctrine entity class make:docker:database Adds a database container to your docker-compose.yaml file make:entity Creates or updates a Doctrine entity class, and optionally an API Platform resource make:fixtures Creates a new class to load Doctrine fixtures make:form Creates a new form class make:message Creates a new meassage and handler make:messenger-middleware Creates a new messenger middleware make:migration Creates a new migration based on database changes make:registration-form Creates a new registration form system make:reset-password Create controller, entity, and repositories for use with symfonycasts/reset-password-bundle make:serializer:encoder Creates a new serializer encoder class …

Slide 16

Slide 16 text

php bin/console make:entity Qweet $ bin/console make:entity Qweet created: src/Entity/Qweet.php created: src/Repository/QweetRepository.php Entity generated! Now let's add some fields! You can always add more fields later manually or by re-running this command. New property name (press to stop adding fields): > userName Field type (enter ? to see all types) [string]: > Field length [255]: > Can this field be null in the database (nullable) (yes/no) [no]:

Slide 17

Slide 17 text

qweetテーブルのmigrationファイル作成し、データベーススキーマを更新
 $ php bin/console make:migration // migrations/Version20230324121000.phpを生成 $ php bin/console doctrine:migrations:migrate // php bin/console d:m:mでもok

Slide 18

Slide 18 text

php bin/console make:crud $ php bin/console make:crud
 
 The class name of the entity to create CRUD (e.g. VictoriousElephant): > Qweet Choose a name for your controller class (e.g. QweetController) [QweetController]: > Do you want to generate tests for the controller?. [Experimental] (yes/no) [no]: > created: src/Controller/QweetController.php created: src/Form/QweetType.php created: templates/qweet/_delete_form.html.twig created: templates/qweet/_form.html.twig created: templates/qweet/edit.html.twig created: templates/qweet/index.html.twig created: templates/qweet/new.html.twig created: templates/qweet/show.html.twig Success! Next: Check your new CRUD by going to /qweet/ タイトルの解答


Slide 19

Slide 19 text

(htmlとcssで見た目をいい感じにする)
 


Slide 20

Slide 20 text

完成 🎉🎉🎉 http://localhost:8000/qweet/

Slide 21

Slide 21 text

コマンドヒストリー(わずか11コマンド)
 / /準備 1 $ composer create-project symfony/skeleton qwitter --no-interaction 2 $ cd qwitter 3 $ php -S localhost:8000 -t public/ 4 $ composer require security twig orm form validator annotations 5 $ cp .env .env.local && vi .env.local 6 $ php bin/console doctrine:database:create / / 本題 7 $ composer require --dev symfony/maker-bundle 8 $ php bin/console make:entity Qweet 9 $ php bin/console doctrine:migration:diff 10 $ php bin/console doctrine:migration:migrate 11 $ php bin/console make:crud

Slide 22

Slide 22 text

さいごに コマンドのみの紹介となりましたが、makeコマンド実行後のdiffをみるだけでもSymfonyこうやっ て書くんだーと感じることができると思います!
 他にも「Symfonyなら〇〇できるよ〜」「〇〇便利だよ〜」とかあれば、ぜひ教えてください!
 LaravelやCakePHP、BEAR.Sundayでも「同じようなのできるよ〜」などの共有も待ってます!


Slide 23

Slide 23 text

ご清聴ありがとうございました!!!

Slide 24

Slide 24 text

参考 ● Console Commands: https://symfony.com/doc/current/console.html ● Artisan Console: https://laravel.com/docs/9.x/artisan ● SymfonyMakerBundle: https://symfony.com/bundles/SymfonyMakerBundle/current/index.html