# 方式一:Symfony CLI (最快) symfony new my_blog --version=lts --webapp # 方式二:Composer composer create-project symfony/skeleton my_blog cd my_blog composer require webapp # 安裝常用套件包
編輯 .env (或 .env.local):
DATABASE_URL="mysql://db_user:db_password@127.0.0.1:3306/my_blog?serverVersion=8.0&charset=utf8mb4"
php bin/console doctrine:database:create
php bin/console make:entity Article # 依提示新增欄位: # title (string, 255, not null) # content (text, not null) # author (string, 100, nullable) # createdAt (datetime, not null) # updatedAt (datetime, nullable)
php bin/console make:crud Article # 產生 Controller、Form、Repository、Twig 模板、路由
php bin/console make:migration php bin/console doctrine:migrations:migrate
symfony server:start -d # 或 php -S localhost:8000 -t public/ # 瀏覽 https://localhost:8000/article/
src/ ├── Controller/ArticleController.php # list, new, show, edit, delete ├── Entity/Article.php # 實體與驗證註解 ├── Form/ArticleType.php # 表單定義 ├── Repository/ArticleRepository.php # 查詢邏輯 templates/ ├── article/ │ ├── index.html.twig # 列表 │ ├── new.html.twig # 新增表單 │ ├── show.html.twig # 詳情 │ └── _form.html.twig # 共用表單片段 config/routes/annotations.yaml # 註解路由載入
| 指令 | 用途 |
|---|---|
php bin/console make:entity | 互動建立/修改實體 |
php bin/console make:crud | 依實體產生完整 CRUD |
php bin/console make:form | 建立表單類別 |
php bin/console doctrine:schema:update --force | 開發環境快速同步結構 |
php bin/console debug:router | 查看所有路由 |
php bin/console cache:clear | 清除快取 |