创建项目

准备工作已做好了,接下来就开始创建一个新的项目。创建项目有两种方法:使用Composer、下载软件包。

不管是用哪种方法都需要用到Composer,因此我们需要先安装ComposerComposer是PHP的一个包管理工具,类似于Python的pip

安装Composer

安装方法也很简单,执行如下命令进行下载安装,并将Composer程序移动到 /usr/local/bin 目录便于之后使用。

  1. $ curl -s https://getcomposer.org/installer | php
  2. $ [sudo] mv composer.phar /usr/local/bin

通过Composer创建项目

Composer安装完成之后,即可通过如下命令创建一个新的Symfony2项目,然后根据提示进行配置。

  1. $ composer.phar create-project symfony/framework-standard-edition symfony_tutorial 2.5.*
  2. Installing symfony/framework-standard-edition (v2.5.5)
  3. - Installing symfony/framework-standard-edition (v2.5.5)
  4. Downloading: 100%
  5. Created project in symfony_tutoriial
  6. Loading composer repositories with package information
  7. Installing dependencies (including require-dev)
  8. - Installing jdorn/sql-formatter (v1.2.17)
  9. Loading from cache
  10. - Installing psr/log (1.0.0)
  11. Loading from cache
  12. - Installing twig/twig (v1.16.0)
  13. Loading from cache
  14. - Installing doctrine/lexer (v1.0)
  15. Loading from cache
  16. - Installing doctrine/annotations (v1.2.1)
  17. Downloading: 100%
  18. - Installing doctrine/collections (v1.2)
  19. Loading from cache
  20. - Installing doctrine/cache (v1.3.1)
  21. Loading from cache
  22. - Installing doctrine/inflector (v1.0)
  23. Loading from cache
  24. - Installing doctrine/common (v2.4.2)
  25. Loading from cache
  26. - Installing symfony/symfony (v2.5.5)
  27. Downloading: 100%
  28. - Installing symfony/icu (v1.2.2)
  29. Downloading: 100%
  30. Downloading: 100%
  31. - Installing doctrine/dbal (v2.4.2)
  32. Loading from cache
  33. - Installing doctrine/doctrine-bundle (v1.2.0)
  34. Loading from cache
  35. - Installing kriswallsmith/assetic (v1.1.2)
  36. Loading from cache
  37. - Installing symfony/assetic-bundle (v2.3.0)
  38. Loading from cache
  39. - Installing doctrine/orm (v2.4.6)
  40. Downloading: 100%
  41. - Installing twig/extensions (v1.1.0)
  42. Loading from cache
  43. - Installing swiftmailer/swiftmailer (v5.3.0)
  44. Downloading: 100%
  45. - Installing symfony/swiftmailer-bundle (v2.3.7)
  46. Loading from cache
  47. - Installing monolog/monolog (1.11.0)
  48. Downloading: 100%
  49. - Installing symfony/monolog-bundle (v2.6.1)
  50. Loading from cache
  51. - Installing sensiolabs/security-checker (v2.0.0)
  52. Downloading: 100%
  53. - Installing sensio/distribution-bundle (v3.0.6)
  54. Downloading: 100%
  55. - Installing sensio/framework-extra-bundle (v3.0.2)
  56. Loading from cache
  57. - Installing incenteev/composer-parameter-handler (v2.1.0)
  58. Loading from cache
  59. - Installing sensio/generator-bundle (v2.4.0)
  60. Downloading: 100%
  61. kriswallsmith/assetic suggests installing leafo/lessphp (Assetic provides the integration with the lessphp LESS compiler)
  62. kriswallsmith/assetic suggests installing leafo/scssphp (Assetic provides the integration with the scssphp SCSS compiler)
  63. kriswallsmith/assetic suggests installing ptachoire/cssembed (Assetic provides the integration with phpcssembed to embed data uris)
  64. kriswallsmith/assetic suggests installing leafo/scssphp-compass (Assetic provides the integration with the SCSS compass plugin)
  65. monolog/monolog suggests installing graylog2/gelf-php (Allow sending log messages to a GrayLog2 server)
  66. monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
  67. monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
  68. monolog/monolog suggests installing ruflin/elastica (Allow sending log messages to an Elastic Search server)
  69. monolog/monolog suggests installing videlalvaro/php-amqplib (Allow sending log messages to an AMQP server using php-amqplib)
  70. monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
  71. monolog/monolog suggests installing ext-mongo (Allow sending log messages to a MongoDB server)
  72. monolog/monolog suggests installing aws/aws-sdk-php (Allow sending log messages to AWS services like DynamoDB)
  73. monolog/monolog suggests installing rollbar/rollbar (Allow sending log messages to Rollbar)
  74. Writing lock file
  75. Generating autoload files
  76. Would you like to install Acme demo bundle? [y/N] y
  77. Creating the "app/config/parameters.yml" file
  78. Some parameters are missing. Please provide them.
  79. database_driver (pdo_mysql):
  80. database_host (127.0.0.1):
  81. database_port (null):
  82. database_name (symfony):
  83. database_user (root):
  84. database_password (null):
  85. mailer_transport (smtp):
  86. mailer_host (127.0.0.1):
  87. mailer_user (null):
  88. mailer_password (null):
  89. locale (en):
  90. secret (ThisTokenIsNotSoSecretChangeIt):
  91. debug_toolbar (true):
  92. debug_redirects (false):
  93. use_assetic_controller (true):
  94. Clearing the cache for the dev environment with debug true
  95. Installing assets as hard copies
  96. Installing assets for Symfony\Bundle\FrameworkBundle into web/bundles/framework
  97. Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution

通过下载软件包创建项目

http://symfony.com/download?v=Symfony_Standard_Vendors_2.5.5.zip 下载软件压缩包,然后解压即可。

  1. $ unzip Symfony_Standard_Vendors_2.5.5.zip

不管是用哪种方法进行安装的,最终项目的目录结构如下:

  1. project_root/
  2. app/ <- 应用程序配置
  3. cache/
  4. config/
  5. logs/
  6. src/ <- 项目代码
  7. ...
  8. vendor/ <- 第三方依赖
  9. ...
  10. web/ <- web根目录
  11. app.php
  12. app_dev.php
  13. ...
  14. composer.json

其中 vendor 目录是存放第三方依赖库的,如果使用的git进行代码的版本管理的话,应该要把它添加到.gitignore文件中。

如果你的项目中 vendor 目录为空,那么需要执行命令 composer.phar install 进行安装第三方依赖库。如果第三方依赖库有更新,那么需要执行命令 composer.phar update 进行更新。

更新配置

项目创建完成之后,接下来是检查环境是否正确以及更新配置信息。

先执行如下命令进行检查:

  1. $ php app/check.php
  2. Symfony2 Requirements Checker
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. > PHP is using the following php.ini file:
  5. /opt/lcphp/lib/php.ini
  6. > Checking Symfony requirements:
  7. ........................................
  8. [OK]
  9. Your system is ready to run Symfony2 projects
  10. Note The command console could use a different php.ini file
  11. ~~~~ than the one used with your web server. To be on the
  12. safe side, please check the requirements from your web
  13. server using the web/config.php script.

环境检查没有问题之后再进行编辑 app/config/parameters.yml 文件,配置数据库信息和邮件信息。