网页 综合 更新时间 2026-11-13 17:27:12

Installing & Setting up the Symfony Framework (Symfony Docs)

自动蜘蛛 symfony.com 正文约 6000 字
Installing & Setting up the Symfony Framework (Symfony Docs)
Do you prefer video tutorials? Check out the Cosmic Coding with Symfony screencast series.
Before creating your first Symfony application you must:
Also, install the Symfony CLI. This is optional, but it gives you a helpful binary called symfony that provides all the tools you need to develop and run your Symfony application locally.
The symfony binary also provides a tool to check if your computer meets all requirements. Open your console terminal and run this command:
The Symfony CLI is open source, and you can contribute to it in the symfony-cli/symfony-cli GitHub repository.
Open your console terminal and run any of these commands to create a new Symfony application:
The only difference between these two commands is the number of packages installed by default. The --webapp option installs extra packages to give you everything you need to build a web application.
If you're not using the Symfony binary, run these commands to create the new Symfony application using Composer:
No matter which command you run to create the Symfony application, all of them will create a new my_project_directory/ directory, download some dependencies into it and even generate the basic directories and files you'll need to get started. In other words, your new application is ready!
The project's cache and logs directory (by default, <project>/var/cache/ and <project>/var/log/) must be writable by the web server. If you have any issues, read how to set up permissions for Symfony applications.
In addition to creating new Symfony projects, you will also work on projects already created by other developers. In that case, you only need to get the project code and install the dependencies with Composer. Assuming your team uses Git, set up your project with the following commands:
You'll probably also need to customize your .env file and do a few other project-specific tasks (e.g. creating a database). When working on an existing Symfony application for the first time, it may be useful to run this command which displays information about the project:
In production, you should install a web server like Nginx or Apache and configure it to run Symfony. This method can also be used if you're not using the Symfony local web server for development.
However, for local development, the most convenient way of running Symfony is by using the local web server provided by the Symfony CLI tool. This local server provides support for HTTP/2, concurrent requests, TLS/SSL and automatic generation of security certificates, among other things.
Open your terminal, move into your new project directory and start the local web server as follows:
Browse your application by visiting the URL displayed by the Symfony server (for example, http://localhost:8000/). You can also open the application automatically:
Learn more about all available commands in the Symfony CLI documentation.
The web server works with any PHP application, not only Symfony projects, so it's a very useful generic development tool.
If you'd like to use Docker with Symfony, see Using Docker with Symfony.
A common practice when developing Symfony applications is to install packages (Symfony calls them bundles) that provide ready-to-use features. Packages usually require some setup before using them (editing some file to enable the bundle, creating some file to add some initial config, etc.)
Most of the time this setup can be automated and that's why Symfony includes Symfony Flex, a tool to simplify the installation/removal of packages in Symfony applications. Technically speaking, Symfony Flex is a Composer plugin that is installed by default when creating a new Symfony application and which automates the most common tasks of Symfony applications.
You can also add Symfony Flex to an existing project.
Symfony Flex modifies the behavior of the require, update and remove Composer commands to provide advanced features. Consider the following example:
If you run that command in a Symfony application which doesn't use Flex, you'll see a Composer error explaining that logger is not a valid package name. However, if the application has Symfony Flex installed, that command installs and enables all the packages needed to use the official Symfony logger.
This is possible because lots of Symfony packages/bundles define "recipes", which are a set of automated instructions to install and enable packages into Symfony applications. Flex keeps track of the recipes it installed in a symfony.lock file, which must be committed to your code repository.
Symfony Flex recipes are contributed by the community and they are stored in two public repositories:
Read the Symfony Recipes documentation to learn everything about how to create recipes for your own packages.
Sometimes a single feature requires installing several packages and bundles. Instead of installing them individually, Symfony provides packs, which are Composer metapackages that include several dependencies.
For example, to add debugging features in your application, you can run the composer require --dev debug command. This installs the symfony/debug-pack, which in turn installs several packages like symfony/debug-bundle, symfony/monolog-bundle, symfony/var-dumper, etc.
You won't see the symfony/debug-pack dependency in your composer.json, as Flex automatically unpacks the pack. This means that it only adds the real packages as dependencies (e.g. you will see a new symfony/var-dumper in require-dev).
The symfony binary created when you installed the Symfony CLI provides a command to check whether your project's dependencies contain any known security vulnerabilities:
A good security practice is to execute this command regularly to be able to update or replace compromised dependencies as soon as possible. The security check is done locally by fetching the public PHP security advisories database, so your composer.lock file is not sent over the network.
The check:security command terminates with a non-zero exit code if any of your depend
访问原文 返回搜索 https://symfony.com/doc/current/setup.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。