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 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
  2. 準備(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
  3. composer create-project symfony/skeleton qwitter --no-interaction (symfony new --dir=qwitter でやってることとほぼ同じです )

    中身が特にないsymfonyプロジェクト(今回はプロジェクト名:qwitter)を作ります
 💡symfony newを使うとgit initしてくれたり.php-versionの作成もしてくれます。

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

    welcom to Symfony 6.2.7の画面 で歓迎してくれるのでやる気が出ます󰵗🫶
 
 http://localhost:8000/

  5. 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 💡新規作成したいデータベース名 
 を指定してください

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

  7. 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 …
  8. 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 <return> 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]:
  9. 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/ タイトルの解答

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