- 安裝兩個recipes
composer req symfony/serializer
compser req friendsofsymfony/rest-bundle
- 創(chuàng)建一個實體虏劲,這里以User代替直砂,并添加相應(yīng)的字段
~ cd 你的工作目錄/
~ bin/console make:entity
Class name of the entity to create or update (e.g. OrangeJellybean):
> User
Add another property? Enter the property name (or press <return> to stop adding fields):
> name
Field type (enter ? to see all types) [string]:
>
Field length [255]:
>
Can this field be null in the database (nullable) (yes/no) [no]:
> no
updated: src/Entity/User.php
Add another property? Enter the property name (or press <return> to stop adding fields):
> pass
- User實例生成后妨托,再根據(jù)類生成相應(yīng)的user表
~ bin/console make:migration
#成功后再執(zhí)行下面的命令
~ bin/console doctrine:migrations:migrate
#輸入y速那,user表就生成了
- 目錄結(jié)構(gòu)
- 修改restbundle配置文件
#config/packages/fost_rest.yaml
fos_rest:
view:
view_response_listener: true
format_listener:
rules:
- { path: ^/api, prefer_extension: true, fallback_format: json, priorities: [ json] }
- { path: ^/, fallback_format: html }
- 修改annotations配置蚊俺,使web路徑和api路徑區(qū)分開
#config/routes/annotations.yaml
#名字不重要,可以隨便命名
web_controller:
resource: ../../src/Controller/Web/
type: annotation
rest_controller:
resource: ../../src/Controller/Rest/
type: annotation
prefix: /api
-
Controller目錄結(jié)構(gòu)
WechatIMG4.jpeg
在Rest下創(chuàng)建了v1這個目錄蔽莱,用于區(qū)分不同版本的api
到這里弟疆,準(zhǔn)備工作已完成。