Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Version:

Installing & Setting up the Symfony Framework

Edit this page

Screencast

Do you prefer video tutorials? Check out theCosmic Coding with Symfonyscreencast series.

Technical Requirements

Before creating your first Symfony application you must:

Also,install the Symfony CLI. This is optional, but it gives you ahelpful binary calledsymfony that provides all tools you need todevelop and run your Symfony application locally.

Thesymfony binary also provides a tool to check if your computer meets allrequirements. Open your console terminal and run this command:

1
$symfony check:requirements

Note

The Symfony CLI is open source, and you can contribute to it in thesymfony-cli/symfony-cli GitHub repository.

Creating Symfony Applications

Open your console terminal and run any of these commands to create a new Symfonyapplication:

12345
# run this if you are building a traditional web application$symfony new my_project_directory --version="7.3.x" --webapp# run this if you are building a microservice, console application or API$symfony new my_project_directory --version="7.3.x"

The only difference between these two commands is the number of packagesinstalled by default. The--webapp option installs extra packages to giveyou everything you need to build a web application.

If you're not using the Symfony binary, run these commands to create the newSymfony application using Composer:

1234567
# run this if you are building a traditional web application$composer create-project symfony/skeleton:"7.3.x" my_project_directory$cd my_project_directory$composer require webapp# run this if you are building a microservice, console application or API$composer create-project symfony/skeleton:"7.3.x" my_project_directory

No matter which command you run to create the Symfony application. All of themwill create a newmy_project_directory/ directory, download some dependenciesinto it and even generate the basic directories and files you'll need to getstarted. In other words, your new application is ready!

Note

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 haveany issue, read how toset up permissions for Symfony applications.

Setting up an Existing Symfony Project

In addition to creating new Symfony projects, you will also work on projectsalready created by other developers. In that case, you only need to get theproject code and install the dependencies with Composer. Assuming your team usesGit, setup your project with the following commands:

1234567
# clone the project to download its contents$cd projects/$gitclone ...# make Composer install the project's dependencies into vendor/$cd my-project/$composer install

You'll probably also need to customize your.env fileand do a few other project-specific tasks (e.g. creating a database). Whenworking on an existing Symfony application for the first time, it may be usefulto run this command which displays information about the project:

1
$php bin/console about

Running Symfony Applications

In production, you should install a web server like Nginx or Apache andconfigure it to run Symfony. Thismethod can also be used if you're not using the Symfony local web server fordevelopment.

However for local development, the most convenient way of running Symfony is byusing thelocal web server provided by theSymfony CLI tool. This local server provides among other things support forHTTP/2, concurrent requests, TLS/SSL and automatic generation of securitycertificates.

Open your console terminal, move into your new project directory and start thelocal web server as follows:

12
$cd my-project/$symfony server:start

Open your browser and navigate tohttp://localhost:8000/. If everything isworking, you'll see a welcome page. Later, when you are finished working, stopthe server by pressingCtrl+C from your terminal.

Tip

The web server works with any PHP application, not only Symfony projects,so it's a very useful generic development tool.

Symfony Docker Integration

If you'd like to use Docker with Symfony, seeUsing Docker with Symfony.

Installing Packages

A common practice when developing Symfony applications is to install packages(Symfony calls thembundles) that provide ready-to-usefeatures. Packages usually require some setup before using them (editing somefile 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 includesSymfony Flex, a tool to simplify the installation/removal of packages inSymfony applications. Technically speaking, Symfony Flex is a Composer pluginthat is installed by default when creating a new Symfony application and whichautomates the most common tasks of Symfony applications.

Symfony Flex modifies the behavior of therequire,update, andremove Composer commands to provide advanced features. Consider thefollowing example:

12
$cd my-project/$composer require logger

If you run that command in a Symfony application which doesn't use Flex, you'llsee a Composer error explaining thatlogger is not a valid package name.However, if the application has Symfony Flex installed, that command installsand 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 intoSymfony applications. Flex keeps track of the recipes it installed in asymfony.lock file, which must be committed to your code repository.

Symfony Flex recipes are contributed by the community and they are stored intwo public repositories:

  • Main recipe repository, is a curated list of recipes for high quality andmaintained packages. Symfony Flex only looks in this repository by default.
  • Contrib recipe repository, contains all the recipes created by thecommunity. All of them are guaranteed to work, but their associated packagescould be unmaintained. Symfony Flex will ask your permission before installingany of these recipes.

Read theSymfony Recipes documentation to learn everything about how tocreate recipes for your own packages.

Symfony Packs

Sometimes a single feature requires installing several packages and bundles.Instead of installing them individually, Symfony providespacks, which areComposer metapackages that include several dependencies.

For example, to add debugging features in your application, you can run thecomposer require --dev debug command. This installs thesymfony/debug-pack,which in turn installs several packages likesymfony/debug-bundle,symfony/monolog-bundle,symfony/var-dumper, etc.

You won't see thesymfony/debug-pack dependency in yourcomposer.json,as Flex automatically unpacks the pack. This means that it only adds the realpackages as dependencies (e.g. you will see a newsymfony/var-dumper inrequire-dev).

Checking Security Vulnerabilities

Thesymfony binary created when you installed theSymfony CLIprovides a command to check whether your project's dependencies contain any known securityvulnerability:

1
$symfony check:security

A good security practice is to execute this command regularly to be able toupdate or replace compromised dependencies as soon as possible. The securitycheck is done locally by fetching the publicPHP security advisories database,so yourcomposer.lock file is not sent on the network.

Thecheck:security command terminates with a non-zero exit code if any ofyour dependencies is affected by a known security vulnerability. This way youcan add it to your project build process and your continuous integrationworkflows to make them fail when there are vulnerabilities.

Tip

In continuous integration services you can check security vulnerabilitiesby running thecomposer audit command. This uses the same data internallyascheck:security but does not require installing the entire Symfony CLIduring CI or on CI workers.

Symfony LTS Versions

According to theSymfony release process,"long-term support" (or LTS for short) versions are published every two years.Check out theSymfony releases to know which is the latest LTS version.

By default, the command that creates new Symfony applications uses the lateststable version. If you want to use an LTS version, add the--version option:

12345678
# use the most recent LTS version$symfony new my_project_directory --version=lts# use the 'next' Symfony version to be released (still in development)$symfony new my_project_directory --version=next# you can also select an exact specific Symfony version$symfony new my_project_directory --version="6.4.*"

Thelts andnext shortcuts are only available when using Symfony tocreate new projects. If you use Composer, you need to tell the exact version:

1
$composer create-project symfony/skeleton:"6.4.*" my_project_directory

The Symfony Demo application

The Symfony Demo Application is a fully-functional application that shows therecommended way to develop Symfony applications. It's a great learning tool forSymfony newcomers and its code contains tons of comments and helpful notes.

Run this command to create a new project based on the Symfony Demo application:

1
$symfony new my_project_directory --demo

Start Coding!

With setup behind you, it's time toCreate your first page in Symfony.

This work, including the code samples, is licensed under aCreative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.3backers


    [8]ページ先頭

    ©2009-2025 Movatter.jp