Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), support for creating new API directories, configuration and environment, testing, vagrant, etc. (1)
1. Create a new warehouse, shuijingwan/yii2-app-advanced, as shown in Figure 1
Yii 2 advanced project template, the template includes four layers: interface (the API to implement the RESTful style web service service), front-end, back-end and console, each of which is a separate YII application.
2. Successfully created the warehouse, as shown in Figure 2
3. Use the following command to install the application: yiisoft/yii2-app-advanced, as shown in Figure 3
composer self-update composer global require "fxp/composer-asset-plugin" composer create-project --prefer-dist yiisoft/yii2-app-advanced github-shuijingwan-yii2-app-advanced
4. Create a new warehouse on the command line and push the existing repository to shuijingwan/yii2-app-advanced, based on the second step, as shown in Figure 4
git init git add . git commit -m "After all commits after installing yiisoft/yii2-app-advanced." Git Remote Add Origin https://github.com/shuijingwan/yii2-app-advanced.git git push -u origin master
5. Open: https://github.com/shuijingwan/yii2-app-advanced, the warehouse has been successfully pushed, as shown in Figure 5
6. Open Windows PowerShell, execute the init command and select dev as the environment, as shown in Figure 6
.\init 0 yes
7. Change the host file to point the domain to your server, configure the C:\Windows\System32\Drivers\etc\hosts file, as shown in Figure 7
# github-shuijingwan-yii2-app-advanced 127.0.0.1 github-shuijingwan-yii2-app-advanced.localhost www.github-shuijingwan-yii2-app-advanced.localhost backend.github-shuijingwan-yii2-app-advanced.localhost api.github-shuijingwan-yii2-app-advanced.localhost
8. Configure the Web server based on Nginx, configure virtual hosts: WWW, Backend, API to suspend configuration, edit C:\nginx-1.10.1\conf\vhosts\github-shuijingwan-yii2-app-advanced.conf
## frontend ##
server {
Charset UTF-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name www.github-shuijingwan-yii2-app-advanced.localhost;
root e:/wwwroot/github-shuijingwan-yii2-app-advanced/frontend/web;
index index.php;
access_log logs/www.github-shuijingwan-yii2-app-advanced.localhost.access.log;
error_log logs/www.github-shuijingwan-yii2-app-advanced.localhost.error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $URI =404;
}
location ~* /\. {
deny all;
}
}
## backend ##
server {
Charset UTF-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name backend.github-shuijingwan-yii2-app-advanced.localhost;
root e:/wwwroot/github-shuijingwan-yii2-app-advanced/backend/web;
index index.php;
access_log logs/backend.github-shuijingwan-yii2-app-advanced.localhost.access.log;
error_log logs/backend.github-shuijingwan-yii2-app-advanced.localhost.error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $URI =404;
}
location ~* /\. {
deny all;
}
}
## misc ##
### www redirect ###
server {
listen 80;
server_name github-shuijingwan-yii2-app-advanced.localhost;
return 301 http://www.github-shuijingwan-yii2-app-advanced.localhost$request_uri;
}
9. Check whether the current PHP environment meets the most basic needs of Yii, copy /requirements.php to /frontend/web/requirements.php, and then access the url through the browser http://www.github-shuijingwan-yii2-app-advanced.localhost/requirements.php , as shown in Figure 8
$frameworkPath = dirname(__file__) ./../../vendor/yiisoft/yii2;
10. Create a new database (g-s-yii2-app-advanced) and adjust the components in /common/config/main-local.php accordingly[‘db’]Configuration, the character set is utf8mb4.
Create userg-s-yii2-app-advancedat%Identified with mysql_native_password as**;grant usage on *.* tog-s-yii2-app-advancedat%require none with max_queries_per_hour 0 max_connections_per_hour 0 max_updates_per_hour 0 max_user_connections 0;create database if not exists `g-s-yii2-app-advanced`; grant all priorityg-s-yii2-app-advancedat%;
db=>[ class=>yii\db\connection, DSN=>mysql:host=localhost;dbname=g-s-yii2-app-advanced, username=>g-s-yii2-app-advanced, Password=>IADO0X7UK4Parrm, Charset=>utf8mb4, ]#ATFP_CLOSE_Translate_span#,
11. Execute the migration command, as shown in Figure 9
.\yii migrate yes
12. Check the database, the sorting rule is: utf8mb4_unicode_ci, as shown in Figure 10
13. Create a test database (g-s-yii2-app-advanced-test) and adjust the components in /common/config/test-local.php accordingly[‘db’]Configuration, the character set is utf8mb4.
Create userg-s-yii2-app-advanced-testat%Identified with mysql_native_password as**;grant usage on *.* tog-s-yii2-app-advanced-testat%require none with max_queries_per_hour 0 max_connections_per_hour 0 max_updates_per_hour 0 max_user_connections 0;create database if not exists `g-s-yii2-app-advanced-test`; grant all priority `g-s-yii2-app-advanced-test`.* tog-s-yii2-app-advanced-testat%;
db=>[ DSN=>mysql:host=localhost;dbname=g-s-yii2-app-advanced-test, username=>g-s-yii2-app-advanced-test, Password=>9dixzevrgn9akpco, Charset=>utf8mb4, ]#ATFP_CLOSE_Translate_span#
14. Execute the migration command and report an error: ExceptionYii\Base\UnknownPropertyExceptionwith messageSetting unknown property: yii\console\request::cookieValidat
ionkeyIn e:\wwwroot\github-shuijingwan-yii2-app-advanced\vendor\yiisoft\yii2\base\component.php:209, as shown in Figure 11
./yii_test migrate
15. Since the console application uses different request components, edit \common\config\test.php, and annotate the request component (uncomment in the web application), as shown in Figure 12
components=>[ user=> [ class=>yii\web\user, IdentityClass=>common\models\user, ]#ATFP_CLOSE_Translate_span#, tiveting request=>[ CookieValidationKey=>Test, ]#ATFP_CLOSE_Translate_span#, */ ],
16. Execute the migration command, build the test suite, and then run all the sample tests, as shown in Figure 13
Note: After executing the migration command (./yii_test migrate), you need to uncomment the request component, otherwise an error will be reported:
test ..\common\tests\unit\models\loginformtest.php:testLoginCorRect [yii\base\InvalidConfigException]yii\web\request::cookieValidationKey must be configured with a secret key.
./yii_test migrate vendor/bin/codecept build vendor/bin/codecept run
17. Check the test database, the sorting rule is: utf8mb4_unicode_ci, as shown in Figure 14
18. Add interface application (API), copy frontend to API, environments/dev/frontend to environments/dev/api and environments/prod/frontend to environments/prod/api, as shown in Figure 15
19. Delete all the files under the directory \API\Runtime\Debug, and adjust the namespace and path in the directory /API to start with the API (replace frontend to API), as shown in Figure 16
20. Add yii::setAlias in Common\Config\Bootstrap.php(API, dirname(dirname(__dir__)) ./api), as shown in Figure 17
21. Edit environments/index.php, search for this file with 6 frontends, then copy 6 copies accordingly, and replace them with api, as shown in Figure 18
22, \vagrant\nginx\app.conf, add virtual host API
\vagrant\nginx\app.conf
server {
Charset UTF-8;
client_max_body_size 128M;
sendfile off;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name y2aa-frontend.test;
root /app/frontend/web/;
index index.php;
access_log /app/vagrant/nginx/log/frontend-access.log;
error_log /app/vagrant/nginx/log/frontend-error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
try_files $URI =404;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
server {
Charset UTF-8;
client_max_body_size 128M;
sendfile off;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name y2aa-backend.test;
root /app/backend/web/;
index index.php;
access_log /app/vagrant/nginx/log/backend-access.log;
error_log /app/vagrant/nginx/log/backend-error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
try_files $URI =404;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
server {
Charset UTF-8;
client_max_body_size 128M;
sendfile off;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name y2aa-api.test;
root /app/api/web/;
index index.php;
access_log /app/vagrant/nginx/log/api-access.log;
error_log /app/vagrant/nginx/log/api-error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
try_files $URI =404;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
23. Edit \vagrant\nginx\log\.gitignore, the log file of the configuration API is ignored
# nginx logs backend-access.log backend-error.log frontend-access.log frontend-error.log api-access.log api-error.log
24. Edit \VagrantFile to add API-related configurations
requireyaml
requireFileUtils
required_plugins = %w( vagrant-hostmanager vagrant-vbguest )
required_plugins.each do |plugin|
Exec "vagrant plugin install #{plugin}" unless vagrant.has_plugin? plugin?
End
domains = {
frontend:y2aa-frontend.test,
backend:y2aa-backend.test,
API:Y2AA-API.Test,
}
config = {
local:./vagrant/config/vagrant-local.yml,
example:./vagrant/config/vagrant-local.example.yml
}
# copy config from example if local config not exists
fileutils.cp config[:example], config[:local]unless file.exist?(config[:local])
# read config
options = yaml.load_file config[:local]
# check github token
if options['github_token'].nil? || options['github_token'].to_s.length != 40
puts "You must place real github token into configuration:\n/yii2-app-advanced/vagrant/config/vagrant-local.yml"
exit
End
# vagrant configure
vagrant.configure(2) do |config|
# select the box
config.vm.box =Bento/Ubuntu-16.04
# should we ask about box updates?
config.vm.box_check_update = options['box_check_update']
config.vm.providervirtualboxdo |vb|
# machine cpus count
vb.cpus = options['cpus']
# Machine Memory Size
vb.memory = options['memory']
# machine name (for virtualbox UI)
vb.name = options['machine_name']
End
# machine name (for vagrant console)
config.vm.define options['machine_name']
# machine name (for guest machine)
config.vm.hostname = options['machine_name']
# network settings
config.vm.networkprivate_network, ip: options['ip']
# sync: folderyii2-app-advanced(host machine) -> folder/app(guest machine)
config.vm.synced_folder./,/app, OWNER:vagrant, group:vagrant
# disable folder/vagrant(guest machine)
config.vm.synced_folder.,/vagrant, disabled: true
# hosts settings (host machine)
config.vm.provision :hostmanager
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true
config.hostmanager.aliases = domains.values
# Provisioners
config.vm.provisionshell, path:./vagrant/provision/once-as-root.sh, args:[options['timezone']]
config.vm.provisionshell, path:./vagrant/provision/once-as-vagrant.sh, args:[options['github_token'], privileged: false
config.vm.provisionshell, path:./vagrant/provision/always-as-root.sh, run:Always
# post-install message (vagrant console)
config.vm.post_up_message = "Frontend URL: http://#{domains[:frontend]}\nbackend url: http://#{domains[:backend]}\NAPI URL: http://#{domains[:api]}"
End
25. Configure the web server based on Nginx, configure virtual hosts: WWW, Backend, API, edit C:\nginx-1.10.1\conf\vhosts\github-shuijingwan-yii2-app-advanced.conf
## frontend ##
server {
Charset UTF-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name www.github-shuijingwan-yii2-app-advanced.localhost;
root e:/wwwroot/github-shuijingwan-yii2-app-advanced/frontend/web;
index index.php;
access_log logs/www.github-shuijingwan-yii2-app-advanced.localhost.access.log;
error_log logs/www.github-shuijingwan-yii2-app-advanced.localhost.error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $URI =404;
}
location ~* /\. {
deny all;
}
}
## backend ##
server {
Charset UTF-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name backend.github-shuijingwan-yii2-app-advanced.localhost;
root e:/wwwroot/github-shuijingwan-yii2-app-advanced/backend/web;
index index.php;
access_log logs/backend.github-shuijingwan-yii2-app-advanced.localhost.access.log;
error_log logs/backend.github-shuijingwan-yii2-app-advanced.localhost.error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $URI =404;
}
location ~* /\. {
deny all;
}
}
## API ##
server {
Charset UTF-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
#listen[::]:80 default_server ipv6only=on; ## listen for ipv6
server_name api.github-shuijingwan-yii2-app-advanced.localhost;
root e:/wwwroot/github-shuijingwan-yii2-app-advanced/api/web;
index index.php;
access_log logs/api.github-shuijingwan-yii2-app-advanced.localhost.access.log;
error_log logs/api.github-shuijingwan-yii2-app-advanced.localhost.error.log;
location / {
# redirect everything that isnt a real file to index.php
try_files $uri $uri//index.php$is_args$args;
}
# uncomment to avoid processing by yii
#location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
# try_files $URI =404;
#}
#error_page 404 /404.html;
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param script_filename $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $URI =404;
}
location ~* /\. {
deny all;
}
}
## misc ##
### www redirect ###
server {
listen 80;
server_name github-shuijingwan-yii2-app-advanced.localhost;
return 301 http://www.github-shuijingwan-yii2-app-advanced.localhost$request_uri;
}
26. Open Windows PowerShell, execute the init command and select dev as the environment, and the environment configuration file required by the API application is automatically generated, as shown in Figure 19
.\init 0 yes yes yes yes yes yes
27. Open: http://api.github-shuijingwan-yii2-app-advanced.localhost/ , as expected, as shown in Figure 20
28. Edit the codeception.yml configuration that contains the tests of all applications, as shown in Figure 21
# global codeception file to run tests from all apps include: - Common -frontend - backend - API paths: log: console/runtime/logs settings: colors: true
29. Run all sample tests, including the API, as shown in Figure 22
vendor/bin/codecept run
30. Edit readme.md and add an API to the directory structure
<p align="center"> <a href="https://github.com/yiisoft" target="_blank"> <img src="https://avatars0.githubusercontent.com/u/993323" height="100px"> </a> <h1 align="center">yii 2 Advanced project template</h1> <br> </p> Yii 2 Advanced Project Template is a Skeleton[Yii 2](http://www.yiiframework.com/) Application best for Developing Complex Web Applications with Multiple Tiers. The Template includes three tiers: front end, back end, and console, each of which Is a separate yii application. The template is designed to work in a team development environment. It supports Deploying the application in different environments. documentation is at[docs/guide/README.md](docs/guide/readme.md). [] (https://packagist.org/packages/yiisoft/yii2-app-advanced) [] (https://packagist.org/packages/yiisoft/yii2-app-advanced) [](https://travis-ci.org/yiisoft/yii2-app-advanced) Directory Structure ------------------- ``` Common config/ contain shared configurations Mail/ Contains View Files for E-Mails Models/ contains models used in both API and backend and frontend Tests/ contains tests for common classes Console config/ contain console configurations Controllers/contains console controls (commands) Migrations/ contains database migrations Models/contains console-specific model classes Runtime/ Contains files generated runtime backend Assets/ contains applications assets such as JavaScript and CSS config/ contain backend configurations Controllers/ contains web controller classes Models/contains backend-specific model classes Runtime/ Contains files generated runtime Tests/ contains tests for backend applications views/ contains views files for the web application Web/ Contains the Entry Script and Web Resources frontend Assets/ contains applications assets such as JavaScript and CSS config/ contain frontend configurations Controllers/ contains web controller classes Models/contains frontend-specific model classes Runtime/ Contains files generated runtime Tests/ contains tests for frontend applications views/ contains views files for the web application Web/ Contains the Entry Script and Web Resources widgets/contains frontend widgets API Assets/ contains applications assets such as JavaScript and CSS config/ contain API configurations Controllers/ contains web controller classes Models/Contains API-Specific Model Classes Runtime/ Contains files generated runtime Tests/ contains tests for API application views/ contains views files for the web application Web/ Contains the Entry Script and Web Resources widgets/ contains api widgets Vendor/ Contains Dependent 3rd-Party Packages environments/contains environment-based overrides ```
31. Test whether to use vagrant to install, whether to support API, reference URL: https://github.com/yiisoft/yii2-app-advance d/blob/master/docs/guide-en-en/start-installation.md , using Vagrant to install (Windows User Manual)
32. Go to the /vagrant/config folder, rename vagrant-local.example.yml as vagrant-local.yml (recommended to copy), and place the github personal API token to vagrant-local.yml, as shown in Figure 23
33. Add the following code to the hosts file
# github-shuijingwan-yii2-app-advanced vagrant 192.168.83.137 y2aa-frontend.test y2aa-backend.test y2aa-api.test
34. Open Windows PowerShell, switch the path to the root directory of the project, and execute the following commands (need to overturn the wall, it may need to be executed multiple times), there is an error during the period, refer to step 15 processing, as shown in Figure 24 and Figure 25
Vagrant plugin install vagrant-hostmanager vagrant up
35. After waiting for completion, just access the following URL in the browser, which is in line with expectations, as shown in Figure 26
frontend: http://y2aa-frontend.test
backend: http://y2aa-backend.test
API: http://y2aa-api.test
36. Restore the modification made in step 32, and edit \vagrant\config\.gitignore
# local configuration vagrant-local.yml

























