This project is an api framework in PHP, based on Yii2. You can take it as a api scaffolding.
The object for this project is to build api easily and funny.
git clone git@github.com:xiaohei2015/php-api-scaffolding.git
php composer.phar install
<?php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=api_scaffolding_v1_0_0',
'username' => 'root',
'password' => '111111',
'charset' => 'utf8',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '@common/mail',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
],
];
init
server{
listen 1081;
root /home/vm05/dongming/src/My/php-api-scaffolding/frontend/web;
server_name 127.0.0.1;
access_log /data/logs/nginx/api-api.scaffolding.com-access.log;
error_log /data/logs/nginx/api-api.scaffolding.com-error.log;
location / {
root /home/vm05/dongming/src/My/php-api-scaffolding/frontend/web;
index index.html index.htm index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
server{
listen 1082;
root /home/vm05/dongming/src/My/php-api-scaffolding/api/web;
server_name 127.0.0.1;
access_log /data/logs/nginx/api-api.scaffolding.com-access.log;
error_log /data/logs/nginx/api-api.scaffolding.com-error.log;
location / {
root /home/vm05/dongming/src/My/php-api-scaffolding/api/web;
index index.html index.htm index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
server{
listen 1083;
root /home/vm05/dongming/src/My/php-api-scaffolding/backend/web;
server_name 127.0.0.1;
access_log /data/logs/nginx/api-api.scaffolding.com-access.log;
error_log /data/logs/nginx/api-api.scaffolding.com-error.log;
location / {
root /home/vm05/dongming/src/My/php-api-scaffolding/backend/web;
index index.html index.htm index.php;
if (!-e $request_filename){
rewrite ^/(.*) /index.php last;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
upstream scaffolding_config{
server 127.0.0.1:1081;
}
upstream scaffolding_api{
server 127.0.0.1:1082;
}
upstream scaffolding_backend{
server 127.0.0.1:1083;
}
server{
listen 80;
server_name api.scaffolding.com;
access_log /data/logs/nginx/api.scaffolding.com-access.log;
error_log /data/logs/nginx/api.scaffolding.com-error.log;
if ($host != 'api.scaffolding.com') {
rewrite ^/(.*)$ http://api.scaffolding.com/$1 permanent;
}
location ~ ^/api\//* {
proxy_pass http://scaffolding_api;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/admin\//* {
proxy_pass http://scaffolding_backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ ^/* {
proxy_pass http://scaffolding_config;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
7. Check whether you can visit URL 'http://api.scaffolding.com/api/v1/user/login'
If system response like '{"code":1,"msg":"page not found。","data":[],"type":"yii\web\NotFoundHttpException"}', it means, your environment has been ready.
Tips: remember to configure your local hosts file, so that when you it can be direct to your local IP when visiting 'api.scaffolding.com'
a) Create models class by gii, e.g. common/models/Article.php
b) Create modelsBiz class by gii, e.g. common/modelsBiz/ArticleBiz.php
c) Create Controller class, e.g. api/modules/v1/controllers/Article2Controller.php,
d) Send request with URL 'http://api.scaffolding.com/v1/article2s', you will get a list api.
e) You can also try below URL
POST 'http://api.scaffolding.com/v1/article2s' with title=title1 and content=content1
GET 'http://api.scaffolding.com/v1/article2s/1'
PUT 'http://api.scaffolding.com/v1/article2s/1' with title=title11 and content=content11
Refer to function createArticle under 'common/modelsBiz/ArticleBiz.php'.
Refer to function createArticle under 'common/modelsBiz/ArticleBiz.php'.
Refer to function actionAdd under 'api/modules/v1/controllers/ArticleController.php'.
When you calling URL 'http://api.scaffolding.com/api/v1/user/login', system will response "token" field to client. And the token will be as the access-token for the following request, otherwise system will reject the response. Please take the access-token in the request header.
a) QQ(306539332)
b) Email(306539332@qq.com)