目录结构 #
一、项目根目录 #
1.1 目录概览 #
text
my_project/
├── bin/ # 可执行文件
├── config/ # 配置文件
├── public/ # 公共资源(Web根目录)
├── src/ # 源代码
├── templates/ # 模板文件
├── tests/ # 测试文件
├── translations/ # 翻译文件
├── var/ # 缓存和日志
├── vendor/ # 第三方依赖
├── .env # 环境变量
├── .env.local # 本地环境变量
├── composer.json # Composer配置
├── composer.lock # 依赖锁定文件
└── symfony.lock # Symfony Flex锁定文件
1.2 核心目录说明 #
| 目录 | 说明 | Git提交 |
|---|---|---|
| bin/ | 可执行文件 | 是 |
| config/ | 配置文件 | 是 |
| public/ | Web入口 | 是 |
| src/ | 业务代码 | 是 |
| templates/ | 模板文件 | 是 |
| tests/ | 测试代码 | 是 |
| var/ | 缓存日志 | 否 |
| vendor/ | 第三方库 | 否 |
二、bin目录 #
2.1 目录结构 #
text
bin/
├── console # Symfony命令行工具
└── phpunit # PHPUnit测试工具
2.2 使用console #
bash
# 运行Symfony命令
php bin/console list
# 查看帮助
php bin/console help make:controller
# 清除缓存
php bin/console cache:clear
# 查看路由
php bin/console debug:router
# 查看服务
php bin/console debug:container
三、config目录 #
3.1 目录结构 #
text
config/
├── packages/ # 组件配置
│ ├── dev/ # 开发环境配置
│ ├── prod/ # 生产环境配置
│ ├── test/ # 测试环境配置
│ ├── doctrine.yaml # Doctrine配置
│ ├── framework.yaml # 框架配置
│ ├── routing.yaml # 路由配置
│ ├── security.yaml # 安全配置
│ ├── twig.yaml # Twig配置
│ └── validator.yaml # 验证配置
├── routes/ # 路由定义
│ ├── annotations.yaml # 注解路由配置
│ └── dev/ # 开发环境路由
├── bundles.php # Bundle注册
├── routes.yaml # 主路由配置
└── services.yaml # 服务配置
3.2 环境配置加载顺序 #
text
1. config/packages/*.yaml # 所有环境
2. config/packages/{env}/*.yaml # 特定环境覆盖
3.3 bundles.php #
php
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
];
3.4 services.yaml #
yaml
# 服务配置
services:
_defaults:
autowire: true
autoconfigure: true
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
App\Controller\:
resource: '../src/Controller/'
tags: ['controller.service_arguments']
四、public目录 #
4.1 目录结构 #
text
public/
├── index.php # 前端控制器(入口文件)
├── build/ # 前端构建资源
├── bundles/ # Bundle资源
└── uploads/ # 上传文件
4.2 index.php入口文件 #
php
<?php
use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;
use Symfony\Component\ErrorHandler\Debug;
use Symfony\Component\HttpFoundation\Request;
require dirname(__DIR__).'/vendor/autoload.php';
(new Dotenv())->bootEnv(dirname(__DIR__).'/.env');
if ($_SERVER['APP_DEBUG']) {
umask(0000);
Debug::enable();
}
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
4.3 Web服务器配置 #
Nginx配置示例:
nginx
server {
listen 80;
server_name example.com;
root /var/www/my_project/public;
location / {
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
location ~ \.php$ {
return 404;
}
}
Apache配置示例:
apache
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/my_project/public
<Directory /var/www/my_project/public>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
五、src目录 #
5.1 目录结构 #
text
src/
├── Controller/ # 控制器
│ └── HomeController.php
├── Entity/ # 实体类
│ └── User.php
├── Repository/ # 仓库类
│ └── UserRepository.php
├── Service/ # 服务类
│ └── UserService.php
├── Form/ # 表单类型
│ └── UserType.php
├── EventSubscriber/ # 事件订阅者
│ └── UserSubscriber.php
├── Security/ # 安全相关
│ └── UserAuthenticator.php
├── DependencyInjection/ # 依赖注入扩展
├── Command/ # 命令行命令
│ └── CreateUserCommand.php
├── DataFixtures/ # 数据填充
│ └── UserFixtures.php
├── Twig/ # Twig扩展
│ └── AppExtension.php
├── Validator/ # 自定义验证器
│ └── ContainsAlphanumericValidator.php
└── Kernel.php # 内核类
5.2 Kernel.php #
php
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
}
5.3 命名空间 #
php
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class HomeController extends AbstractController
{
public function index(): Response
{
return new Response('Hello Symfony!');
}
}
六、templates目录 #
6.1 目录结构 #
text
templates/
├── base.html.twig # 基础模板
├── home/ # 首页模板
│ └── index.html.twig
├── user/ # 用户模板
│ ├── index.html.twig
│ ├── show.html.twig
│ └── edit.html.twig
├── form/ # 表单模板
│ └── fields.html.twig
└── bundles/ # Bundle模板覆盖
└── TwigBundle/
└── Exception/
└── error404.html.twig
6.2 base.html.twig示例 #
twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
七、tests目录 #
7.1 目录结构 #
text
tests/
├── Controller/ # 控制器测试
│ └── HomeControllerTest.php
├── Entity/ # 实体测试
│ └── UserTest.php
├── Service/ # 服务测试
│ └── UserServiceTest.php
├── Repository/ # 仓库测试
│ └── UserRepositoryTest.php
└── bootstrap.php # 测试引导文件
7.2 测试示例 #
php
<?php
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class HomeControllerTest extends WebTestCase
{
public function testIndex(): void
{
$client = static::createClient();
$crawler = $client->request('GET', '/');
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Welcome');
}
}
八、var目录 #
8.1 目录结构 #
text
var/
├── cache/ # 缓存文件
│ ├── dev/ # 开发环境缓存
│ └── prod/ # 生产环境缓存
├── log/ # 日志文件
│ └── dev.log
└── sessions/ # Session文件
8.2 缓存管理 #
bash
# 清除缓存
php bin/console cache:clear
# 清除并预热缓存
php bin/console cache:clear --warmup
# 仅预热缓存
php bin/console cache:warmup
九、vendor目录 #
9.1 目录说明 #
text
vendor/
├── composer/ # Composer相关
├── symfony/ # Symfony组件
├── doctrine/ # Doctrine库
├── twig/ # Twig库
├── autoload.php # 自动加载文件
└── ... # 其他第三方库
9.2 注意事项 #
text
警告:
- vendor目录由Composer管理,不要手动修改
- 不要将vendor提交到Git
- 部署时运行composer install安装依赖
十、translations目录 #
10.1 目录结构 #
text
translations/
├── messages.en.yaml # 英文翻译
├── messages.zh_CN.yaml # 中文翻译
├── validators.en.yaml # 验证消息翻译
└── security.en.yaml # 安全消息翻译
10.2 翻译文件示例 #
yaml
# translations/messages.zh_CN.yaml
Welcome: 欢迎
Hello %name%: 你好 %name%
user:
login: 登录
logout: 登出
register: 注册
十一、环境文件 #
11.1 .env文件 #
bash
# .env
APP_ENV=dev
APP_SECRET=your-secret-key-here
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/db_name"
MAILER_DSN=smtp://localhost
11.2 环境文件优先级 #
text
.env.{env}.local → .env.{env} → .env.local → .env
(最高) (最低)
十二、总结 #
本章学习了:
- 项目根目录结构
- bin目录和console命令
- config配置目录
- public入口目录
- src源代码目录
- templates模板目录
- tests测试目录
- var缓存日志目录
- vendor第三方依赖
- translations翻译目录
下一章将学习 第一个应用。
最后更新:2026-03-28