Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

kin29
March 24, 2023

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

PHPerKaigi 2023/03/25 12:10〜 Track A スポンサーセッション(20分)

kin29

March 24, 2023
Tweet

More Decks by kin29

Other Decks in Programming

Transcript

  1. 【Symfony超入門】

    コマンドだけでCRUD画面を作るチート法

    志賀 彩乃


    View Slide

  2. このスライドのゴール(私のもくろみ)
    SymfonyはLaravelと比べ、

    難しそうで疎遠されがち(個人の感想です)


    ↓


    「そんなことない」ってことを知ってもらう💡


    ↓


    Symfony信者を増やして、情報交換がしたいです🙇🙇🙇


    View Slide

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

    View Slide

  4. スタート!


    View Slide

  5. 準備(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

    View Slide

  6. composer create-project symfony/skeleton qwitter --no-interaction
    (symfony new --dir=qwitter でやってることとほぼ同じです
    )
    中身が特にないsymfonyプロジェクト(今回はプロジェクト名:qwitter)を作ります

    💡symfony newを使うとgit initしてくれたり.php-versionの作成もしてくれます。


    View Slide

  7. php -S localhost:8000 -t public/
    (symfony server:start がやってることとほぼ同じはず)
    一旦、webサイトをローカルで確認してみましょう!

    → welcom to Symfony 6.2.7の画面 で歓迎してくれるのでやる気が出ます󰵗🫶


    http://localhost:8000/


    View Slide

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


    View Slide

  9. 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
    💡新規作成したいデータベース名

    を指定してください


    View Slide

  10. php bin/console doctrine:database:create
    直前で設定したDATEBASE_URLのデータベースを作成してくれます。

    よって、わざわざポスグレやMySQLに入ってcreate database db_nameする必要はありませ
    ん!

    View Slide

  11. 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にわか)


    View Slide

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


    View Slide

  13. 本題に入ります!


    View Slide

  14. composer require --dev symfony/maker-bundle
    ControllerやCommand、Entityを作ってくれるコマンドです!

    本日のメインです。

    Laravelでいうphp artisan make みたいなやつだと思ってます(by Laravelにわか)


    View Slide

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

    View Slide

  16. 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]:

    View Slide

  17. qweetテーブルのmigrationファイル作成し、データベーススキーマを更新

    $ php bin/console make:migration // migrations/Version20230324121000.phpを生成
    $ php bin/console doctrine:migrations:migrate // php bin/console d:m:mでもok

    View Slide

  18. 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/
    タイトルの解答


    View Slide

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


    View Slide

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

    View Slide

  21. コマンドヒストリー(わずか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

    View Slide

  22. さいごに
    コマンドのみの紹介となりましたが、makeコマンド実行後のdiffをみるだけでもSymfonyこうやっ
    て書くんだーと感じることができると思います!

    他にも「Symfonyなら〇〇できるよ〜」「〇〇便利だよ〜」とかあれば、ぜひ教えてください!

    LaravelやCakePHP、BEAR.Sundayでも「同じようなのできるよ〜」などの共有も待ってます!


    View Slide

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

    View Slide

  24. 参考
    ● 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

    View Slide