Symfony 安裝簡易教學 (debian stretch)

2018-03-07

Installing Symfony should be easy though

Photo by bruce mars from Pexels

我按照官網說明將symfony安裝完了,但是他不會動。我覺得這個世界對我有著滿滿的惡意angry

 

同學請冷靜 我們這次就來看看symfony安裝時會出現哪些問題吧

0. 軟體安裝需求

由於目前最新版本為4.1,但4.0以後php要求為7.1以上,而debian stretch 目前php只支援到7.0,所以大家可以依照自己os安裝的php版本來決定要安裝那一版的symfony,簡單整理如下:

debian stretch (php7.0)  =>  symfony 3.4

debian buster (>php7.1) => symfony 4.1

當然如果你會自己compile php7.1 library 就不在此限制範圍內smiley

 

1.安裝symfony前置作業

因為在安裝symfony時會經常用到兩個指令composer 以及 symfony,所以就先來安裝這兩個吧

symfony︰

用途說明︰使用symfony 2或3 建構網站時官方建議的安裝工具

安裝詳細介紹︰https://symfony.com/doc/3.4/setup.html


 sudo mkdir -p /usr/local/bin
 sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
 sudo chmod a+x /usr/local/bin/symfony

這邊可能又會遇到一個問題就是curl指令找不到 ><

如果發生這個問題,請執行以下指令後,繼續完成前面的安裝步驟

sudo apt install curl

composer :

用途說明︰基本上在製作網站常常會使用到現成的library而library有時可能會有版本過舊或是和現有library衝突的情況發生,這個composer就是有能力可以來處理這些麻煩的問題,讓你可以放心使用安裝library而不怕彼此衝突

安裝詳細介紹︰https://getcomposer.org/download/

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

此處程式碼為安裝說明用。請勿使用此處的程式碼安裝,因為hash_file比對碼有可能更動,詳細請至composer 官網查看

這邊完成以後會看到有一個 composer.phar 檔案 但因為還沒安裝到 /usr/local/bin 所以目前還沒辦法像symfony安裝完就可以在console中打symfony就可以直接執行,所以這邊提供兩種解法︰

1.把上面第三行換成

php composer-setup.php --install-dir=usr/local/bin --filename=composer

2.參考symfony安裝方法將目前的composer.phar安裝到  /usr/local/bin

 sudo mv composer.phar /usr/local/bin/composer
 sudo chmod a+x /usr/local/bin/composer

這邊有注意到的人會發現為什麼我們要把composer.phar 改名成 composer?

因為之後你在看symfony 或是其他網頁教學,很少有機會看到教學文章中提到 composer.phar 這個指令,大多數時間你只會看到composer,而他們其實代表的視同一個指令 ,所以為了後續操作方便以及學習更容易,改名是有需要的。

 

2. My first symfony project 

這一步就是關鍵所在,後來我在官網上看了一下發現其實有兩種作法

4.0 以前 筆者之前在建立新網站是用以下方法

https://symfony.com/doc/3.4/setup.html

由於目前已更新到4.1所以預設會是以4.1來建立新網站,這邊請加上symfony版本數(3.4)

symfony new my-project 3.4

如果不想透過symfony安裝,也可以使用以下方法

composer create-project symfony/framework-standard-edition my-project "3.4.*"

4.0 以後 官網似乎鼓勵使用 composer create-project 方式來架構新網站

https://symfony.com/doc/current/setup.html

composer create-project symfony/website-skeleton my-project

好笑的是在download,4.0以後官方建議以下方式建立新網站

https://symfony.com/download

composer create-project symfony/skeleton my_project

所以這樣一共就有四種安裝方式 crying

其中差異我們在下一段再談

先講講第一個建立的網站(使用4.0以前建立方法)如何使用內建php-server來看結果

 cd my-project/
 php bin/console server:run

這時候console會提示說網頁可以到哪邊觀看

將該網址貼到瀏覽器後就可以看到我們的第一頁

first page shown after project created

至於如何製作第一頁我們就留到下次再做介紹吧。

 

3. 差異比較

symfony new test_1 3.4

這個安裝基本上照說明進行就可以順利完成

 Preparing project...

 ✔  Symfony 3.4.6 was successfully installed. Now you can:

    * Change your current directory to /home/XXX/Desktop/test/test_1

    * Configure your application in app/config/parameters.yml file.

    * Run your application:
        1. Execute the php bin/console server:start command.
        2. Browse to the http://localhost:8000 URL.

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


composer create-project symfony/framework-standard-edition test_2 "3.4.*"

這個安裝基本上照說明進行就可以順利完成

Installing symfony/framework-standard-edition (v3.4.6)
  - Installing symfony/framework-standard-edition (v3.4.6): Downloading (100%)         
Created project in test_2
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 38 installs, 0 updates, 0 removals
  - Installing doctrine/lexer (v1.0.1): Loading from cache
  - Installing doctrine/annotations (v1.2.7): Downloading (100%)         
  - Installing twig/twig (v1.35.2): Downloading (100%)         
  - Installing paragonie/random_compat (v2.0.11): Loading from cache
  - Installing symfony/polyfill-php70 (v1.7.0): Downloading (100%)         
  - Installing symfony/polyfill-util (v1.7.0): Downloading (100%)         
  - Installing symfony/polyfill-php56 (v1.7.0): Downloading (100%)         
  - Installing symfony/polyfill-mbstring (v1.7.0): Loading from cache
  - Installing symfony/symfony (v3.4.6): Downloading (100%)         
  - Installing symfony/polyfill-intl-icu (v1.7.0): Loading from cache
  - Installing symfony/polyfill-apcu (v1.7.0): Loading from cache
  - Installing psr/simple-cache (1.0.1): Downloading (100%)         
  - Installing psr/log (1.0.2): Loading from cache
  - Installing psr/link (1.0.0): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing fig/link-util (1.0.0): Loading from cache
  - Installing doctrine/inflector (v1.1.0): Downloading (100%)         
  - Installing doctrine/collections (v1.3.0): Downloading (100%)         
  - Installing doctrine/cache (v1.6.2): Loading from cache
  - Installing doctrine/common (v2.6.2): Downloading (100%)         
  - Installing jdorn/sql-formatter (v1.2.17): Loading from cache
  - Installing doctrine/doctrine-cache-bundle (1.3.2): Loading from cache
  - Installing doctrine/dbal (v2.5.13): Loading from cache
  - Installing doctrine/doctrine-bundle (1.8.1): Loading from cache
  - Installing doctrine/instantiator (1.0.5): Loading from cache
  - Installing doctrine/orm (v2.5.14): Loading from cache
  - Installing incenteev/composer-parameter-handler (v2.1.3): Downloading (100%)         
  - Installing composer/ca-bundle (1.1.0): Loading from cache
  - Installing sensiolabs/security-checker (v4.1.8): Downloading (100%)         
  - Installing sensio/distribution-bundle (v5.0.21): Loading from cache
  - Installing sensio/framework-extra-bundle (v5.1.6): Loading from cache
  - Installing monolog/monolog (1.23.0): Loading from cache
  - Installing symfony/monolog-bundle (v3.2.0): Downloading (100%)         
  - Installing swiftmailer/swiftmailer (v5.4.9): Downloading (100%)         
  - Installing symfony/swiftmailer-bundle (v2.6.7): Loading from cache
  - Installing sensio/generator-bundle (v3.1.7): Loading from cache
  - Installing symfony/phpunit-bridge (v3.4.6): Downloading (100%)         
paragonie/random_compat suggests installing ext-libsodium (Provides a modern crypto API that can be used to generate random bytes.)
symfony/polyfill-intl-icu suggests installing ext-intl (For best performance)
doctrine/doctrine-cache-bundle suggests installing symfony/security-acl (For using this bundle to cache ACLs)
sensio/framework-extra-bundle suggests installing symfony/psr-http-message-bridge (To use the PSR-7 converters)
monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing mongodb/mongodb (Allow sending log messages to a MongoDB server via PHP Driver)
monolog/monolog suggests installing php-amqplib/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
monolog/monolog suggests installing php-console/php-console (Allow sending log messages to Google Chrome)
monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
monolog/monolog suggests installing sentry/sentry (Allow sending log messages to a Sentry server)
symfony/phpunit-bridge suggests installing ext-zip (Zip support is required when using bin/simple-phpunit)
Generating autoload files
> Incenteev\ParameterHandler\ScriptHandler::buildParameters
Creating the "app/config/parameters.yml" file
Some parameters are missing. Please provide them.
database_host (127.0.0.1): 
database_port (null): 
database_name (symfony): 
database_user (root): 
database_password (null): 
mailer_transport (smtp): 
mailer_host (127.0.0.1): 
mailer_user (null): 
mailer_password (null): 
secret (ThisTokenIsNotSoSecretChangeIt): 
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::buildBootstrap
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache

 // Clearing the cache for the dev environment with debug                       
 // true                                                                        

                                                                                
 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.    
                                                                                

> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installAssets

 Trying to install assets as relative symbolic links.

                                                                                
 [OK] No assets were provided by any bundle.                                    
                                                                                

> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::installRequirementsFile
> Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::prepareDeploymentTarget

composer create-project symfony/website-skeleton test_3 

這次資料夾結構就有些不同

1.沒有app

2.沒有web

而至於首頁無法顯示是因為

1. 不存在對應的頁面 (之前有appbundle/defaultcontroller.php負責顯示此頁面,這次沒有安裝這個檔案,所以自然是連不上此頁面)

同時也可以透過 php bin/console debug:router 觀察到並不存在此路徑

Installing symfony/website-skeleton (v3.4.4)
  - Installing symfony/website-skeleton (v3.4.4): Loading from cache
Created project in test_3
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 89 installs, 0 updates, 0 removals
  - Installing ocramius/package-versions (1.2.0): Loading from cache
  - Installing symfony/flex (v1.0.70): Loading from cache

Prefetching 32 packages 
  - Downloading (100%)

  - Installing doctrine/lexer (v1.0.1): Loading from cache
  - Installing doctrine/inflector (v1.2.0): Loading from cache
  - Installing doctrine/collections (v1.4.0): Loading from cache
  - Installing doctrine/cache (v1.6.2): Loading from cache
  - Installing doctrine/annotations (v1.4.0): Loading from cache
  - Installing doctrine/common (v2.7.3): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.7.0): Loading from cache
  - Installing symfony/yaml (v3.4.6): 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 (v3.4.6): Loading from cache
  - Installing symfony/filesystem (v3.4.6): Loading from cache
  - Installing symfony/config (v3.4.6): Loading from cache
  - Installing symfony/http-foundation (v3.4.6): Loading from cache
  - Installing symfony/event-dispatcher (v3.4.6): Loading from cache
  - Installing psr/log (1.0.2): Loading from cache
  - Installing symfony/debug (v3.4.6): Loading from cache
  - Installing symfony/http-kernel (v3.4.6): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing symfony/dependency-injection (v3.4.6): 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 (v3.4.6): Loading from cache
  - Installing symfony/validator (v3.4.6): Loading from cache
  - Installing symfony/serializer (v3.4.6): Loading from cache
  - Installing symfony/inflector (v3.4.6): Loading from cache
  - Installing symfony/property-info (v3.4.6): Loading from cache
  - Installing symfony/property-access (v3.4.6): Loading from cache
  - Installing psr/simple-cache (1.0.1): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing symfony/polyfill-apcu (v1.7.0): Loading from cache
  - Installing symfony/cache (v3.4.6): 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 (v3.4.6): Loading from cache
  - Installing symfony/security-bundle (v3.4.6): Loading from cache
  - Installing symfony/process (v3.4.6): Loading from cache
  - Installing monolog/monolog (1.23.0): Loading from cache
  - Installing symfony/monolog-bridge (v3.4.6): Loading from cache
  - Installing symfony/monolog-bundle (v3.2.0): Loading from cache
  - Installing symfony/routing (v3.4.6): Loading from cache
  - Installing symfony/finder (v3.4.6): Loading from cache
  - Installing symfony/class-loader (v3.4.6): Loading from cache
  - Installing symfony/framework-bundle (v3.4.6): Loading from cache
  - Installing symfony/console (v3.4.6): Loading from cache
  - Installing zendframework/zend-eventmanager (3.2.0): Loading from cache
  - Installing zendframework/zend-code (3.1.0): Loading from cache
  - Installing ocramius/proxy-manager (2.0.4): Loading from cache
  - Installing doctrine/dbal (v2.5.13): Loading from cache
  - Installing doctrine/migrations (v1.5.0): Loading from cache
  - Installing symfony/doctrine-bridge (v3.4.6): 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.0.5): Loading from cache
  - Installing doctrine/orm (v2.5.14): Loading from cache
  - Installing symfony/orm-pack (v1.0.5): Loading from cache
  - Installing symfony/options-resolver (v3.4.6): Loading from cache
  - Installing symfony/polyfill-intl-icu (v1.7.0): Loading from cache
  - Installing symfony/intl (v3.4.6): Loading from cache
  - Installing symfony/form (v3.4.6): Loading from cache
  - Installing symfony/expression-language (v3.4.6): Loading from cache
  - Installing symfony/var-dumper (v3.4.6): Loading from cache
  - Installing symfony/phpunit-bridge (v3.4.6): Loading from cache
  - Installing twig/twig (v2.4.6): Loading from cache
  - Installing symfony/twig-bridge (v3.4.6): Loading from cache
  - Installing symfony/web-profiler-bundle (v3.4.6): Loading from cache
  - Installing symfony/twig-bundle (v3.4.6): Loading from cache
  - Installing symfony/stopwatch (v3.4.6): 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 (v3.4.6): Loading from cache
  - Installing symfony/debug-pack (v1.0.4): Loading from cache
  - Installing symfony/asset (v3.4.6): Loading from cache
  - Installing sensio/framework-extra-bundle (v5.1.6): Loading from cache
  - Installing symfony/dom-crawler (v3.4.6): Loading from cache
  - Installing symfony/browser-kit (v3.4.6): Loading from cache
  - Installing symfony/css-selector (v3.4.6): Loading from cache
  - Installing symfony/dotenv (v3.4.6): 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 (a4bdff63f5fbfdd018d5869c7cceef35)
  - 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 (>=3.3): 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

 

composer create-project symfony/skeleton test_4 

主要vendor內容只留下最精簡內容,應該是給進階使用者開發用
 

Installing symfony/skeleton (v3.4.5)
  - Installing symfony/skeleton (v3.4.5): Loading from cache
Created project in test_4
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 23 installs, 0 updates, 0 removals
  - Installing symfony/flex (v1.0.70): Loading from cache
  - Installing psr/log (1.0.2): Loading from cache
  - Installing symfony/debug (v3.4.6): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.7.0): Loading from cache
  - Installing symfony/console (v3.4.6): Loading from cache
  - Installing symfony/routing (v3.4.6): Loading from cache
  - Installing symfony/yaml (v3.4.6): Loading from cache
  - Installing symfony/framework-bundle (v3.4.6): Loading from cache
  - Installing symfony/http-foundation (v3.4.6): Loading from cache
  - Installing symfony/event-dispatcher (v3.4.6): Loading from cache
  - Installing symfony/http-kernel (v3.4.6): Loading from cache
  - Installing symfony/finder (v3.4.6): Loading from cache
  - Installing symfony/filesystem (v3.4.6): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing symfony/dependency-injection (v3.4.6): Loading from cache
  - Installing symfony/config (v3.4.6): Loading from cache
  - Installing symfony/class-loader (v3.4.6): Loading from cache
  - Installing psr/simple-cache (1.0.1): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing symfony/polyfill-apcu (v1.7.0): Loading from cache
  - Installing symfony/cache (v3.4.6): Loading from cache
  - Installing symfony/dotenv (v3.4.6): Loading from cache
Writing lock file
Generating autoload files
Symfony operations: 4 recipes (0ddd9f25a0fe7f6017a9646bccfc603b)
  - Configuring symfony/flex (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/framework-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/console (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/routing (>=3.3): 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