See the Elephant

1992生まれのプログラマが書くエンジニアブログ

symfony4 + Reactで自分のサイトを作って見たい

自分のサイトを作ってみたい

Symfony4 + Reactでゆるふわにアプリを作ってみたい. ヘッドホンが好きなのでヘッドホンをレコメンドするWebアプリを作ってみよう. 仕事でコードを書く時間とは別に自分のためにコードを書いてみたい

どうせならということで新しい環境で作って見たい. PHP 7.2, Symfony4.1 + React(むずかったらVue2系)で作ろう.

導入

まずはphpを入れよう. phpbrewを入れる.

$ brew install phpbrew
$ phpbrew install 7.2.2 +openssl=$(brew --prefix openssl)
===> phpbrew will now build 7.2.2
===> Loading and resolving variants...
Checking distribution checksum...
Checksum matched:
===> Distribution file was successfully extracted, skipping...
===> Checking patches...
Checking patch for replace apache php module name with custom version name
Found existing build.log, renaming it to /Users/shuzon/.phpbrew/build/php-7.2.2/build.log.1519736915
===> Configuring 7.2.2...


Use tail command to see what's going on:
   $ tail -F /Users/shuzon/.phpbrew/build/php-7.2.2/build.log


===> Checking patches...
Checking patch for php5.3.29 multi-sapi patch.
Checking patch for php5.3.x on 64bit machine when intl is enabled.
Checking patch for openssl dso linking patch
3 changes patched.
===> Building...
Build finished: 4.5 minutes.
Installing...
---> Creating php-fpm.conf
---> Creating php.ini
---> Copying /Users/shuzon/.phpbrew/build/php-7.2.2/php.ini-development
---> Found date.timezone is not set, patching...
---> Found date.timezone, patching config timezone with Asia/Tokyo
Congratulations! Now you have PHP with 7.2.2 as php-7.2.2

* To configure your installed PHP further, you can edit the config file at
    /Users/shuzon/.phpbrew/php/php-7.2.2/etc/php.ini

* WARNING:
  You haven't setup your .bashrc file to load phpbrew shell script yet!
  Please run 'phpbrew init' to see the steps!

To use the newly built PHP, try the line(s) below:

    $ phpbrew use php-7.2.2

Or you can use switch command to switch your default php to php-7.2.2:

    $ phpbrew switch php-7.2.2

Enjoy!

$ phpbrew init
Using root: /Users/shuzon/.phpbrew
Initialization successfully finished!
<=====================================================>
Phpbrew environment is initialized, required directories are created under

    /Users/shuzon/.phpbrew

Paste the following line(s) to the end of your ~/.bashrc and start a
new shell, phpbrew should be up and fully functional from there:

    source /Users/shuzon/.phpbrew/bashrc

To enable PHP version info in your shell prompt, please set PHPBREW_SET_PROMPT=1
in your `~/.bashrc` before you source `~/.phpbrew/bashrc`

    export PHPBREW_SET_PROMPT=1

To enable .phpbrewrc file searching, please export the following variable:

    export PHPBREW_RC_ENABLE=1


For further instructions, simply run `phpbrew` to see the help message.

Enjoy phpbrew at $HOME!!

$ phpbrew switch php-7.2.2

php 7.2.2が入った

symfony4を入れる

http://symfony.com/doc/master/setup.html めっちゃこけた

$ composer create-project symfony/website-skeleton headphone-recommend
PHP Fatal error:  Uncaught Error: Call to undefined function Composer\Json\json_decode() in phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Json/JsonFile.php:153
Stack trace:
#0 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Factory.php(290): Composer\Json\JsonFile->validateSchema(1)
#1 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Factory.php(576): Composer\Factory->createComposer(Object(Composer\IO\ConsoleIO), './composer.json', false)
#2 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(338): Composer\Factory::create(Object(Composer\IO\ConsoleIO), NULL, false)
#3 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(451): Composer\Console\Application->getComposer(false, false)
#4 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(149): Composer\Console\Application->getPluginCommands()
#5 phar:///usr/lo in phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Json/JsonFile.php on line 153

Fatal error: Uncaught Error: Call to undefined function Composer\Json\json_decode() in phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Json/JsonFile.php:153
Stack trace:
#0 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Factory.php(290): Composer\Json\JsonFile->validateSchema(1)
#1 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Factory.php(576): Composer\Factory->createComposer(Object(Composer\IO\ConsoleIO), './composer.json', false)
#2 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(338): Composer\Factory::create(Object(Composer\IO\ConsoleIO), NULL, false)
#3 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(451): Composer\Console\Application->getComposer(false, false)
#4 phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Console/Application.php(149): Composer\Console\Application->getPluginCommands()
#5 phar:///usr/lo in phar:///usr/local/Cellar/composer/1.6.3/libexec/composer.phar/src/Composer/Json/JsonFile.php on line 153

requirement

http://symfony.com/doc/master/reference/requirements.html

うまく動かない

phpbrew install 7.1.3 +openssl=$(brew --prefix openssl)

うーむ, だめだ

phpbrewではうまく動かない

$ brew install homebrew/php/php72
$ echo 'export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH"' > ~/.zshrc
$ php -v
PHP 7.2.2 (cli) (built: Feb  1 2018 11:50:40) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

これで動いた.

$ composer create-project symfony/website-skeleton headphone-recommend
Installing symfony/website-skeleton (v4.0.3)
  - Installing symfony/website-skeleton (v4.0.3): Downloading (100%)
Created project in headphone-recommend
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 88 installs, 0 updates, 0 removals
  - Installing ocramius/package-versions (1.3.0): Downloading (100%)
  - Installing symfony/flex (v1.0.70): Downloading (100%)

Prefetching 86 packages 🎵
  - Downloading (100%)

  - Installing doctrine/lexer (v1.0.1): Loading from cache
  - Installing doctrine/inflector (v1.3.0): Loading from cache
  - Installing doctrine/collections (v1.5.0): Loading from cache
  - Installing doctrine/cache (v1.7.1): Loading from cache
  - Installing doctrine/annotations (v1.6.0): Loading from cache
  - Installing doctrine/common (v2.8.1): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.7.0): Loading from cache
  - Installing symfony/http-foundation (v4.0.4): Loading from cache
  - Installing symfony/yaml (v4.0.4): Loading from cache
  - Installing symfony/webpack-encore-pack (v1.0.2): Loading from cache
  - Installing psr/link (1.0.0): Loading from cache
  - Installing fig/link-util (1.0.0): Loading from cache
  - Installing symfony/web-link (v4.0.4): Loading from cache
  - Installing symfony/filesystem (v4.0.4): Loading from cache
  - Installing symfony/config (v4.0.4): Loading from cache
  - Installing symfony/http-kernel (v4.0.4): Loading from cache
  - Installing symfony/event-dispatcher (v4.0.4): Loading from cache
  - Installing psr/log (1.0.2): Loading from cache
  - Installing symfony/debug (v4.0.4): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing symfony/dependency-injection (v4.0.4): Loading from cache
  - Installing egulias/email-validator (2.1.3): Loading from cache
  - Installing swiftmailer/swiftmailer (v6.0.2): Loading from cache
  - Installing symfony/swiftmailer-bundle (v3.2.0): Loading from cache
  - Installing symfony/translation (v4.0.4): Loading from cache
  - Installing symfony/validator (v4.0.4): Loading from cache
  - Installing symfony/serializer (v4.0.4): Loading from cache
  - Installing symfony/inflector (v4.0.4): Loading from cache
  - Installing symfony/property-info (v4.0.4): Loading from cache
  - Installing symfony/property-access (v4.0.4): Loading from cache
  - Installing psr/simple-cache (1.0.0): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing symfony/cache (v4.0.4): Loading from cache
  - Installing webmozart/assert (1.3.0): Loading from cache
  - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
  - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
  - Installing phpdocumentor/reflection-docblock (4.3.0): Loading from cache
  - Installing symfony/serializer-pack (v1.0.1): Loading from cache
  - Installing symfony/security (v4.0.4): Loading from cache
  - Installing symfony/security-bundle (v4.0.4): Loading from cache
  - Installing symfony/process (v4.0.4): Loading from cache
  - Installing monolog/monolog (1.23.0): Loading from cache
  - Installing symfony/monolog-bridge (v4.0.4): Loading from cache
  - Installing symfony/monolog-bundle (v3.1.2): Loading from cache
  - Installing symfony/routing (v4.0.4): Loading from cache
  - Installing symfony/finder (v4.0.4): Loading from cache
  - Installing symfony/framework-bundle (v4.0.4): Loading from cache
  - Installing symfony/console (v4.0.4): Loading from cache
  - Installing zendframework/zend-eventmanager (3.2.0): Loading from cache
  - Installing zendframework/zend-code (3.3.0): Loading from cache
  - Installing ocramius/proxy-manager (2.2.0): Loading from cache
  - Installing doctrine/dbal (v2.6.3): Loading from cache
  - Installing doctrine/migrations (v1.6.2): Loading from cache
  - Installing symfony/doctrine-bridge (v4.0.4): Loading from cache
  - Installing doctrine/doctrine-cache-bundle (1.3.2): Loading from cache
  - Installing jdorn/sql-formatter (v1.2.17): Loading from cache
  - Installing doctrine/doctrine-bundle (1.8.1): Loading from cache
  - Installing doctrine/doctrine-migrations-bundle (v1.3.1): Loading from cache
  - Installing doctrine/instantiator (1.1.0): Loading from cache
  - Installing doctrine/orm (v2.6.0): Loading from cache
  - Installing symfony/orm-pack (v1.0.5): Loading from cache
  - Installing symfony/options-resolver (v4.0.4): Loading from cache
  - Installing symfony/intl (v4.0.4): Loading from cache
  - Installing symfony/polyfill-intl-icu (v1.7.0): Loading from cache
  - Installing symfony/form (v4.0.4): Loading from cache
  - Installing symfony/expression-language (v4.0.4): Loading from cache
  - Installing symfony/polyfill-php72 (v1.7.0): Loading from cache
  - Installing symfony/var-dumper (v4.0.4): Loading from cache
  - Installing symfony/phpunit-bridge (v4.0.4): Loading from cache
  - Installing twig/twig (v2.4.4): Loading from cache
  - Installing symfony/twig-bridge (v4.0.4): Loading from cache
  - Installing symfony/web-profiler-bundle (v4.0.4): Loading from cache
  - Installing symfony/twig-bundle (v4.0.4): Loading from cache
  - Installing symfony/stopwatch (v4.0.4): Loading from cache
  - Installing symfony/profiler-pack (v1.0.3): Loading from cache
  - Installing easycorp/easy-log-handler (v1.0.4): Loading from cache
  - Installing symfony/debug-bundle (v4.0.4): Loading from cache
  - Installing symfony/debug-pack (v1.0.4): Loading from cache
  - Installing symfony/asset (v4.0.4): Loading from cache
  - Installing sensio/framework-extra-bundle (v5.1.6): Loading from cache
  - Installing symfony/dom-crawler (v4.0.4): Loading from cache
  - Installing symfony/browser-kit (v4.0.4): Loading from cache
  - Installing symfony/css-selector (v4.0.4): Loading from cache
  - Installing symfony/dotenv (v4.0.4): Loading from cache
  - Installing symfony/maker-bundle (v1.1.1): Loading from cache
Writing lock file
Generating autoload files
ocramius/package-versions:  Generating version class...
ocramius/package-versions: ...done generating version class
Symfony operations: 20 recipes (3447d53bbbd23ec42f0eca52a31cb31b)
  - Configuring symfony/flex (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/framework-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring doctrine/annotations (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/webpack-encore-pack (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/swiftmailer-bundle (>=2.5): From github.com/symfony/recipes:master
  - Configuring symfony/translation (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/security-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/monolog-bundle (>=3.1): From github.com/symfony/recipes:master
  - Configuring symfony/routing (>=4.0): From github.com/symfony/recipes:master
  - Configuring symfony/console (>=3.3): From github.com/symfony/recipes:master
  - Configuring doctrine/doctrine-cache-bundle (>=1.3.2): From auto-generated recipe
  - Configuring doctrine/doctrine-bundle (>=1.6): From github.com/symfony/recipes:master
  - Configuring doctrine/doctrine-migrations-bundle (>=1.2): From github.com/symfony/recipes:master
  - Configuring symfony/phpunit-bridge (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/web-profiler-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/twig-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring easycorp/easy-log-handler (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/debug-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring sensio/framework-extra-bundle (>=4.0): From github.com/symfony/recipes:master
  - Configuring symfony/maker-bundle (>=1.0): From github.com/symfony/recipes:master
Executing script cache:clear [OK]
Executing script assets:install --symlink --relative public [OK]

Some files may have been created or updated to configure your new packages.
Please review, edit and commit them: these files are yours.


 What's next?


  * Run your application:
    1. Change to the project directory
    2. Execute the php -S 127.0.0.1:8000 -t public command;
    3. Browse to the http://localhost:8000/ URL.

       Quit the server with CTRL-C.
       Run composer require server --dev for a better web server.

  * Read the documentation at https://symfony.com/doc


 Database Configuration


  * Modify your DATABASE_URL config in .env

  * Configure the driver (mysql) and
    server_version (5.7) in config/packages/doctrine.yaml


 How to test?


  * Write test cases in the tests/ folder
  * Run php bin/phpunit

かなり長くなったので今回はここまで