Installing & Setting up the Symfony Framework

Screencast

スクリーンキャスト

Do you prefer video tutorials? Check out the Harmonious Development with Symfony screencast series.

ビデオチュートリアルが好きですか? Harmonious Development with Symfonyscreencast シリーズをチェックしてください。

Technical Requirements

Before creating your first Symfony application you must:

最初の Symfony アプリケーションを作成する前に、次のことを行う必要があります。
  • Install PHP 8.1 or higher and these PHP extensions (which are installed and enabled by default in most PHP 8 installations): Ctype, iconv, PCRE, Session, SimpleXML, and Tokenizer;
    PHP 8.1 以降と次の PHP 拡張機能 (ほとんどの PHP 8 インストールではデフォルトでインストールされ有効になっています) をインストールします: Ctype、iconv、PCRE、Session、SimpleXML、および Tokenizer。
  • Install Composer, which is used to install PHP packages.
    PHP パッケージのインストールに使用する Composer をインストールします。

Optionally, you can also install Symfony CLI. This creates a binary called symfony that provides all the tools you need to develop and run your Symfony application locally.

オプションで、Symfony CLI をインストールすることもできます。これにより、Symfony アプリケーションをローカルで開発および実行するために必要なすべてのツールを提供する symfony というバイナリが作成されます。

The symfony binary also provides a tool to check if your computer meets all requirements. Open your console terminal and run this command:

symfony バイナリは、コンピューターがすべての要件を満たしているかどうかをチェックするツールも提供します。コンソール ターミナルを開き、次のコマンドを実行します。
1
$ symfony check:requirements

Note

ノート

The Symfony CLI is written in Go and you can contribute to it in the symfony-cli/symfony-cli GitHub repository.

Symfony CLI は Go で記述されており、symfony-cli/symfony-cli GitHub リポジトリで貢献できます。

Creating Symfony Applications

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

コンソール ターミナルを開き、次のコマンドのいずれかを実行して、新しい Symfony アプリケーションを作成します。
1
2
3
4
5
# run this if you are building a traditional web application
$ symfony new my_project_directory --version="6.2.*" --webapp

# run this if you are building a microservice, console application or API
$ symfony new my_project_directory --version="6.2.*"

The only difference between these two commands is the number of packages installed by default. The --webapp option installs all the packages that you usually need to build web applications, so the installation size will be bigger.

これら 2 つのコマンドの唯一の違いは、デフォルトでインストールされるパッケージの数です。 --webapp オプションは、Web アプリケーションを構築するために通常必要なすべてのパッケージをインストールするため、インストール サイズが大きくなります。

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

Symfony バイナリを使用していない場合は、次のコマンドを実行して、Composer を使用して newSymfony アプリケーションを作成します。
1
2
3
4
5
6
7
# run this if you are building a traditional web application
$ composer create-project symfony/skeleton:"6.2.*" 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:"6.2.*" my_project_directory

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!

Symfony アプリケーションを作成するためにどのコマンドを実行しても構いません。それらはすべて、新しい my_project_directory/ ディレクトリを作成し、いくつかの依存関係をそこにダウンロードし、開始するために必要な基本的なディレクトリとファイルを生成します。つまり、新しいアプリケーションの準備ができました!

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 have any issue, read how to set up permissions for Symfony applications.

プロジェクトのキャッシュおよびログ ディレクトリ (デフォルトでは、/var/cache/および /var/log/) は、Web サーバーによって書き込み可能である必要があります。問題がある場合は、Symfony アプリケーションのアクセス許可を設定する方法をお読みください。

Setting up an Existing Symfony Project

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, setup your project with the following commands:

新しい Symfony プロジェクトの作成に加えて、他の開発者によって既に作成されたプロジェクトにも取り組みます。その場合、プロジェクト コードを取得し、Composer で依存関係をインストールするだけで済みます。チームが Git を使用していると仮定すると、次のコマンドを使用してプロジェクトをセットアップします。
1
2
3
4
5
6
7
# clone the project to download its contents
$ cd projects/
$ git clone ...

# make Composer install the project's dependencies into vendor/
$ cd my-project/
$ composer install

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 a existing Symfony application for the first time, it may be useful to run this command which displays information about the project:

また、おそらく .env ファイルをカスタマイズし、その他のプロジェクト固有のタスク (データベースの作成など) をいくつか行う必要があります。既存の Symfony アプリケーションで初めて作業する場合、プロジェクトに関する情報を表示する次のコマンドを実行すると便利な場合があります。
1
$ php bin/console about

Running Symfony Applications

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.

本番環境では、Nginx や Apache などの Web サーバーをインストールし、Symfony を実行するように構成する必要があります。この方法は、開発に Symfony ローカル Web サーバーを使用していない場合にも使用できます。

However for local development, the most convenient way of running Symfony is by using the local web server provided by the symfony binary. This local server provides among other things support for HTTP/2, concurrent requests, TLS/SSL and automatic generation of security certificates.

ただし、ローカル開発の場合、Symfony を実行する最も便利な方法は、symfony バイナリによって提供されるローカル Web サーバーを使用することです。このローカル サーバーは、とりわけ、HTTP/2、同時要求、TLS/SSL、およびセキュリティ証明書の自動生成のサポートを提供します。

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

コンソール ターミナルを開き、新しいプロジェクト ディレクトリに移動して、次のようにローカル Web サーバーを起動します。
1
2
$ cd my-project/
$ symfony server:start

Open your browser and navigate to http://localhost:8000/. If everything is working, you'll see a welcome page. Later, when you are finished working, stop the server by pressing Ctrl+C from your terminal.

ブラウザーを開き、http://localhost:8000/ に移動します。すべてが機能している場合は、ウェルカム ページが表示されます。後で、作業が終了したら、端末から Ctrl+C を押してサーバーを停止します。

Tip

ヒント

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

Web サーバーは、Symfony プロジェクトだけでなく、あらゆる PHP アプリケーションで動作するため、非常に便利な汎用開発ツールです。

Symfony Docker Integration

If you'd like to use Docker with Symfony, see Using Docker with Symfony

Symfony で Docker を使用する場合は、Symfony で Docker を使用するを参照してください。

Installing Packages

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.)

Symfony アプリケーションを開発する際の一般的な方法は、すぐに使用できる機能を提供するパッケージ (Symfony ではバンドルと呼ばれます) をインストールすることです。パッケージは通常、使用する前にセットアップが必要です (バンドルを有効にするためにファイルを編集する、初期設定を追加するためにファイルを作成するなど)。

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.

ほとんどの場合、このセットアップは自動化できます。そのため、Symfony には、Symfony アプリケーションでのパッケージのインストール/削除を簡素化するツールである Symfony Flex が含まれています。技術的に言えば、Symfony Flex は Composer プラグインであり、新しい Symfony アプリケーションを作成するときにデフォルトでインストールされ、Symfony アプリケーションの最も一般的なタスクを自動化します。

Tip

ヒント

You can also add Symfony Flex to an existing project.

Symfony Flex を既存のプロジェクトに追加することもできます。

Symfony Flex modifies the behavior of the require, update, and remove Composer commands to provide advanced features. Consider the following example:

Symfony Flex は、require、update、および remove Composer コマンドの動作を変更して、高度な機能を提供します。次の例を検討してください。
1
2
$ cd my-project/
$ composer require logger

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.

Flex を使用しない Symfony アプリケーションでそのコマンドを実行すると、ロガーが有効なパッケージ名ではないことを説明する Composer エラーが表示されます。ただし、アプリケーションに Symfony Flex がインストールされている場合、そのコマンドは必要なすべてのパッケージをインストールして有効にします公式の Symfony ロガーを使用します。

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 パッケージ/バンドルが「レシピ」を定義しているためです。これは、パッケージを Symfony アプリケーションにインストールして有効にするための一連の自動化された手順です。 Flex は、インストールしたレシピを asymfony.lock ファイルに記録します。このファイルは、コード リポジトリにコミットする必要があります。

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

Symfony Flex のレシピはコミュニティによって提供され、2 つのパブリック リポジトリに保存されます。
  • Main recipe repository, is a curated list of recipes for high quality and maintained packages. Symfony Flex only looks in this repository by default.
    メインのレシピ リポジトリは、高品質で管理されたパッケージの厳選されたレシピのリストです。デフォルトでは、Symfony Flex はこのリポジトリのみを検索します。
  • Contrib recipe repository, contains all the recipes created by the community. All of them are guaranteed to work, but their associated packages could be unmaintained. Symfony Flex will ask your permission before installing any of these recipes.
    Contrib レシピ リポジトリには、コミュニティによって作成されたすべてのレシピが含まれています。それらはすべて動作することが保証されていますが、関連するパッケージは維持されない可能性があります。 Symfony Flex は、これらのレシピをインストールする前に許可を求めます。

Read the Symfony Recipes documentation to learn everything about how to create recipes for your own packages.

Symfony Recipes のドキュメントを読んで、独自のパッケージのレシピを作成する方法をすべて学んでください。

Symfony Packs

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.

場合によっては、1 つの機能で複数のパッケージとバンドルをインストールする必要があります。Symfony は、個別にインストールする代わりに、いくつかの依存関係を含む Composer メタパッケージであるパックを提供します。

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.

たとえば、アプリケーションにデバッグ機能を追加するには、composer require --dev debug コマンドを実行できます。これにより、symfony/debug-pack がインストールされ、symfony/debug-bundle、symfony/monolog-bundle、symfony/var-dumper などのいくつかのパッケージがインストールされます。

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). While it is not recommended, you can use the composer require --no-unpack ... option to disable unpacking.

Flex がパックを自動的にアンパックするため、composer.json に symfony/debug-pack の依存関係は表示されません。これは、実際のパッケージのみを依存関係として追加することを意味します (たとえば、require-dev に新しい symfony/var-dumper が表示されます)。お勧めしませんが、 composerrequire --no-unpack ... オプションを使用してアンパックを無効にすることができます。

Checking Security Vulnerabilities

The symfony binary created when you install Symfony CLI provides a command to check whether your project's dependencies contain any known security vulnerability:

Symfony CLI をインストールするときに作成される symfony バイナリは、プロジェクトの依存関係に既知のセキュリティ脆弱性が含まれているかどうかを確認するコマンドを提供します。
1
$ symfony check:security

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 on the network.

適切なセキュリティ プラクティスは、このコマンドを定期的に実行して、侵害された依存関係をできるだけ早く更新または置換できるようにすることです。セキュリティ チェックは、公開されている PHP セキュリティ アドバイザリ データベースを取得することによってローカルで行われるため、composer.lock ファイルはネットワーク上に送信されません。

The check:security command terminates with a non-zero exit code if any of your dependencies is affected by a known security vulnerability. This way you can add it to your project build process and your continuous integration workflows to make them fail when there are vulnerabilities.

依存関係のいずれかが既知のセキュリティ脆弱性の影響を受ける場合、check:security コマンドはゼロ以外の終了コードで終了します。このようにして、プロジェクトのビルド プロセスと継続的インテグレーション ワークフローに追加して、脆弱性がある場合にそれらを失敗させることができます。

Tip

ヒント

In continuous integration services you can check security vulnerabilities using a different stand-alone project called Local PHP Security Checker. This is the same project used internally by check:security but much smaller in size than the entire Symfony CLI.

継続的インテグレーション サービスでは、Local PHP Security Checker と呼ばれる別のスタンドアロン プロジェクトを使用して、セキュリティの脆弱性をチェックできます。

Symfony LTS Versions

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

Symfony のリリース プロセスに従って、「長期サポート」(略して LTS) バージョンは 2 年ごとに公開されます。Symfony のリリースをチェックして、最新の LTS バージョンを確認してください。

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

デフォルトでは、新しい Symfony アプリケーションを作成するコマンドは lateststable バージョンを使用します。 LTS バージョンを使用する場合は、 --version オプションを追加します。
1
2
3
4
5
6
7
8
# 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="5.4.*"

The lts and next shortcuts are only available when using Symfony to create new projects. If you use Composer, you need to tell the exact version:

lts および next ショートカットは、Symfony を使用して新しいプロジェクトを作成する場合にのみ使用できます。 Composer を使用している場合は、正確なバージョンを伝える必要があります。
1
$ composer create-project symfony/skeleton:"5.4.*" my_project_directory

The Symfony Demo application

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

Symfony デモ アプリケーションは、Symfony アプリケーションを開発するための推奨される方法を示す、完全に機能するアプリケーションです。これは Symfony の初心者にとって優れた学習ツールであり、そのコードには大量のコメントと役立つメモが含まれています。

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

次のコマンドを実行して、Symfony Demo アプリケーションに基づいて新しいプロジェクトを作成します。
1
$ symfony new my_project_directory --demo

Start Coding!

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

セットアップが完了したら、Symfony で最初のページを作成します。

Learn More