caxy / symfony-starter-kit
Requires
- php: >=5.5.9
- api-platform/core: ^2.0
- doctrine/doctrine-bundle: ^1.6
- doctrine/doctrine-cache-bundle: ^1.2
- doctrine/orm: ^2.5
- friendsofsymfony/user-bundle: ~2.0@dev
- incenteev/composer-parameter-handler: ^2.0
- lexik/jwt-authentication-bundle: ^2.1
- sensio/distribution-bundle: ^5.0
- sensio/framework-extra-bundle: ^3.0.2
- sonata-project/admin-bundle: ^3.10
- sonata-project/doctrine-orm-admin-bundle: ^3.1
- symfony/monolog-bundle: ^3.0
- symfony/polyfill-apcu: ^1.0
- symfony/swiftmailer-bundle: ^2.3
- symfony/symfony: 3.2.*
Requires (Dev)
- doctrine/doctrine-fixtures-bundle: ^2.3
- nelmio/api-doc-bundle: ^2.13
- sensio/generator-bundle: ^3.0
- symfony/phpunit-bridge: ^3.0
This package is auto-updated.
Last update: 2024-09-22 07:53:22 UTC
README
用于与 Angular 前端解耦项目一起使用的解耦后端 Starter Kit
### 开始使用
### 1. 克隆仓库
git clone --bare git@github.com:caxy/symfony-starter-kit.git
cd symfony-starter-kit
git push --mirror {NEW_GITHUB_REPO_LINK}
cd ../
rm -rf symfony-starter-kit
git clone {NEW_GITHUB_REPO_LINK}
cd {NEW_REPO_NAME}
### 2. 安装包并填写参数
composer install
bash dev-setup.sh
### 3. 设置数据库并加载固定数据
bin/console d:d:c
bin/console d:s:c
bin/console d:f:l -n
### 4. 设置权限(高级)
请参阅 Symfony 书籍中的 设置权限。
macOS
HTTPDUSER=`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
sudo chmod -R +a "$HTTPDUSER allow delete,write,append,file_inherit,directory_inherit" var
sudo chmod -R +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" var
Linux/BSD
HTTPDUSER=`ps axo user,comm | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\ -f1`
#if this doesn't work, try adding `-n` option
sudo setfacl -R -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var
sudo setfacl -dR -m u:"$HTTPDUSER":rwX -m u:`whoami`:rwX var
### 5. JWT 设置
mkdir -p var/jwt # For Symfony3+, no need of the -p option
openssl genrsa -out var/jwt/private.pem -aes256 4096
openssl rsa -pubout -in var/jwt/private.pem -out var/jwt/public.pem
### 6. Apache 用户的重要注意事项
Apache 服务器将移除任何不在有效 HTTP BASIC AUTH 格式的 Authorization 标头。
如果您打算使用此包的授权头模式(您应该这样做),请将这些规则添加到您的 VirtualHost 配置中
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
### 7. 确认
以最高详细程度运行 Symfony 内置的 Web 服务器。
bin/console server:run -vvv
然后运行一个应返回认证错误的命令
curl http://localhost:8000/api/users/1
响应应该是
{"code":401,"message":"Invalid credentials"}
如果 PHP 配置不当,它可能在以上步骤中的任何步骤中报告错误,并包括更改 php.ini
文件的方向。
### 8. 测试 API
要测试内置的测试,请运行
phpunit
首先从终端生成 JWT 令牌
#username can be any user in the database, user, admin, superadmin are available from the fixutres.
bin/console generate:token {username}
输出将给您一个长字符串,将其复制并粘贴到输入框中 {INSERT TOKEN HERE} 的 URL
要测试 API,请浏览到 localhost:8000/api/doc?bearer={INSERT TOKEN HERE}。请勿与 localhost:8000/api 混淆,它们是分开的,并且没有 JWT 令牌将无法工作。
一旦在 localhost:8000/api/doc?bearer={INSERT TOKEN HERE} 上,您需要在右上角的输入框中再次设置令牌,并在其中说 'api key:' 并按保存。然后您可以使用端点。