php - yii2 setup
安装
1
2
3
4
5
# 基础包 一般推荐这里开始
composer create-project yiisoft/yii2-app-basic yiibase 2.0.12
# 前后台集成的专业包
composer create-project yiisoft/yii2-app-advanced yiiadv 2.0.12
目录权限
1
2
3
chmod 777 runtime
chmod 777 web/assets
chmod 0755 yii
数据迁移
- 配置数据库连接
打开 1
common\config\main-local.php
- 创建迁移表
1
./yii migrate/create create_blog_table
blog 是表名
文件位置1
advanced\console\migrations
- 执行迁移
1
./yii migrate
gii 代码生成器
需要开启开发者模式
http://localhost:8888/yiiadv/frontend/web/index.php?r=gii
api 调试工具 1
postman
1 | postman |
https://www.getpostman.com/apps
路由地址
- 开启
1
cponfig\web.php
1
2
3
4
5
6
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
],
],
- nginx配置
1
2
cd /usr/local/nginx/conf/vhost
vi xxxxx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name reson.com;
location / {
root D:/wwwroot/reson/web;
index index.html index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
}
location ~ \.php$ {
root D:/wwwroot/reson/web;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
- apache配置
1
.htaccess
1
2
3
4
5
6
7
8
9
10
11
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
部署
- 修改
1
web/index.php
1
2
3
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
www 权限1
runtime
1
chown -R www:www runtime/
- 修改跨域目录
1
vi /usr/local/nginx/conf/fastcgi.conf
- 开发环境的部署脚本
1
2
3
4
5
6
7
8
#!/bin/bash
#./bin/dev/deploy.sh
composer install # 它来把 composer 所有的依赖安装好
./init --env=Development --overwrite=y # 重新初始化环境配置
# 等等 我们还有许多别的,例如: bower install ,因为我们的静态资源是使用的 bower 来管理的
- 线上部署脚本
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
# file ./bin/prod/deploy.sh
# ... ...
composer install --prefer-dist --no-dev --no-progress --optimize-autoloader
# 初始化 php 环境变量
./init --env=Production --overwrite=y
# git submodule
git submodule init
git submodule update
控制器 post 关闭 1
Csrf
1 | Csrf |
1
2
3
4
5
6
7
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'EGETcj5HFf1_krlY-gb3rtM5Vcp2mAeS',
'enableCookieValidation' => false,
'enableCsrfValidation' => false,
],
superagent post 收不到
1
2
3
4
5
6
7
8
9
function toy_update (data, callback) {
superagent
.post("/fix-title")
.type('form')
.send(data)
.end(function (err, res) {
callback(err, res);
});
}
需要设置 1
.type('form')
AR
http://www.yiichina.com/doc/api/2.0/yii-db-activequery http://www.yiichina.com/doc/api/2.0/yii-db-query http://www.yiichina.com/doc/guide/2.0/db-query-builder