Install and set up the Symfony framework
Welcome to Symfony! Starting a new Symfony project is easy, and your first working Symfony program can be configured in minutes.
To make creating new programs easier, Symfony provides an installer. Downloading it is your first step.
Installing Symfony Installer ¶
Using Symfony Installer is the only recommended way to create a new Symfony project. The installer is just a php program. You only need to install it once to create an unlimited number of Symfony programs.
The installer requires php5.4 or higher. If you are still using legacy php5.3, you cannot use the Symfony installer.
If you are using a packaged solution such as WAMP, XAMP or MAMP, please check whether they use a recent version of PHP. You can use the following command on the command line to check the PHP version:
$ php --version
Depending on the operating system, the installation method of the Symfony installer is also different.
Linux and MAC OS X systems ¶
Open the command line tool and execute the following command:
$ sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony $ sudo chmod a+x /usr/local/bin/symfony
This will create a global Symfony command to your system.
Windows system ¶
On Windows, execute the following command:
c:\>php -r "readfile('http://symfony.com/installer');" > symfony
It will download a symfony
file and then move the file to In the folder where you want to create the Symfony project, you can boot various installations by running the following command:
c:\>move symfony c:\projects c:\projects\> php symfony
Creating a Symfony program ¶
After the Symfony installer is set up, use Brand new command new
to create a Symfony project:
# Linux, Mac OS X $ symfony new my_project_name # Windows c:\> cd projects/ c:\projects\> php symfony new my_project_name
This command creates a new my_project_name
directory, which is based on New project for the latest stable version of Symfony program. In addition, the installer will also check whether your system has the technical requirements to execute Symfony programs. If it is not met, you will see a list of requirements for "what needs to be changed".
For security reasons, all Symfony versions are digitally signed before distribution. If you need to verify the version number integrated into the program, follow the steps in this article.
If the installer does not work or outputs nothing, make sure your system has the phar extension installed and enabled.
Make your program run on a specific Symfony version ¶
In case you need to run the project on a specific Symfony version, use the new
command with the second option Parameters:
# use the most recent version in any Symfony branch 不同分支的最新版本 $symfony new my_project_name 2.8 $symfony new my_project_name 3.0 # use a specific Symfony version 指定版本 $symfony new my_project_name 2.7.3$ symfony new my_project_name 2.8.1 # use a beta or RC version (useful for testing new Symfony versions) 新版可用于测试 $symfony new my_project 3.0.0-BETA1$ symfony new my_project 2.7.0-RC1
The installer also supports a special versionlts
, which is the latest long-term maintenance version Symfony LTS version
$ symfony new my_project_name lts
Refer to the Symfony release process to understand in depth why there are Which versions of the framework exist and which one is better suited for your project.
Create Symfony programs without using the installer ¶
If you are still using php5.3, or you cannot execute the installer for some reason, you can install Symfony based on composer.
Composer is a dependency manager that is widely used by modern PHP programs. It can also be used to install the Symfony framework. If you don't need to install composer globally, please read below.
Install Composer globally ¶
Please refer to Installing Composer globally.
Use Composer to create a Symfony program ¶
If you have installed Composer, execute the create-project
command to create a Symfony program based on the stable version :
$ composer create-project symfony/framework-standard-edition my_project_name
If you need to specify the version, provide the version number as the second parameter of create-project
$ composer create-project symfony/framework-standard-edition my_project_name "3.0.*"
If your network connection is slow Slow, you might think Composer isn't doing anything. At this time, add the -vvv
flag to the preceding command to output the details of everything Composer is doing.
Running Symfony ¶
When developing, Symfony utilizes PHP's built-in web server. Therefore, run the Symfony program in the directory where the project is located and execute the following command:
$ cd my_project_name/ $ php bin/console server:run
Then, open the browser and access the http://localhost:8000/
link to see the Symfony welcome page :
In addition to the welcome page, you may also see blank pages and error pages, which are usually caused by misconfiguration of directory permissions. Depending on your operating system, there are several solutions. This is explained in the "Setting Permissions" section.
PHP built-in server only exists in php5.4 and above. If it is an old version of PHP, you should configure a virtual host (Virtual Host) for the Symfony project on the web server of the operating system.
server:run
The command is only suitable for the development stage. If you use a traditional web server such as Apache or Nginx, refer to the article Configuring a web server.
Check the configuration of the Symfony running environment ¶
The Symfony program has a built-in server environment checker to show whether the relevant settings are suitable for Symfony. Check it out by visiting the link below:
http://localhost:8000/config.php
If there are display issues, fix them before continuing to use Symfony.
Update Symfony program ¶
At this point, you have created a fully functional Symfony project that can be put into development immediately. Symfony programs rely on a large number of external class libraries, which can be downloaded to the vendor/
directory and are exclusively managed by Composer.
Frequently updating these third-party libraries is a best practice to prevent bugs and vulnerabilities. Execute Composer's update
command to update them all at once:
$ cd my_project_name/ $ composer update
Depending on the complexity of the project, this update method will take several minutes to complete.
Symfony also provides a command for checking whether project dependencies contain known major security vulnerabilities:
$ php bin/console security:check
It is a good habit to execute this command regularly, Because those immunocompromised dependencies can be updated or replaced as quickly as possible.
Install the Symfony demo program ¶
The Symfony demo program is fully functional and demonstrates the recommended development approach. It is intended as a learning tool for Symfony beginners, with extensive comments and helpful tips in the source code.
To download the demo program, from anywhere on your system, execute the demo
command of the Symfony installer:
# Linux, Mac OS X $ symfony demo # Windows c:\projects\> php symfony demo
Once downloaded, enter symfony_demo
directory, and run the PHP built-in web server, execute the php bin/console server:run
command, and then access the browser's http://localhost:8000
link to start Use the Symfony demo program.
Install the Symfony distribution package ¶
The "distribution package" of the Symfony project refers to a full-featured program, including the Symfony core class library, a set of necessary bundles, and a set of reasonable Directory structure, and some default configurations. In fact, when you create the Symfony program as described above, you have downloaded the default distribution package provided by Symfony - called Symfony Standard Edition (Symfony Standard Edition).
Symfony Standard Edition is currently the most popular distribution and the first choice for Symfony developers. However, the Symfony community also provides other distribution packages for you to choose from.
Symfony CMF Standard Edition is the preferred distribution package that requires Symfony CMF. CMF is an open source project based on Symfony, which facilitates developers to add CMS (content management system) functions to the framework.
Symfony REST Edition shows how to build a RESTFul API program. This distribution integrates the FOSRestBundle and several other related bundles.
Use source control ¶
If you are using a version control system like Git, you can safely commit all your project code. This is because the Symfony program already contains a .gitignore
file specially prepared for Symfony.
For an introduction to "how good" hosting your project code in Git is, see How to Create and Store a Symfony Project in Git.
Check versioned Symfony programs ¶
When you use Composer to manage program dependencies, it is recommended to ignore the entire vendor
file when submitting code to the repository. folder. This means that when you view the Symfony program from the Git repository, there is no vendor
directory in it, and of course the entire program is difficult to start.
To make the program run, check the Symfony program, and then execute the install
This Composer command downloads and installs all the dependencies required for the program:
$ cd my_project_name/ $ composer install
Why Composer knows to Which special dependencies should be installed? This is because when the Symfony program is submitted to the repository, the composer.json
and composer.lock
files are also submitted. These two files tell Composer which dependencies (and their specific versions) are required when installing Symfony.
Start developing ¶
Now that you have a fully functional Symfony program, you can start developing! Your distribution package includes some sample code - look at the README.md
file in the program (open with Notepad) for details of the routines in the distribution package.
If you are new to Symfony, refer to Creating Your First Symfony Page, where you can learn how to create pages, change configurations, and anything else you want to do in a new program.