Yii2 Restful 添加 Api 应用

1 在项目的根目录下复制一份 backend 为 api:

[ahcj@localhost yii2-app-advanced]$ cp -a backend api
[ahcj@localhost yii2-app-advanced]$ cp -a environments/dev/frontend environments/dev/api
[ahcj@localhost yii2-app-advanced]$ cp -a environments/prod/frontend environments/prod/api

2 把 api 目录 下的 所有 backend 命名空间替换为 api

3 修改 common/config/bootstrap.php,添加添加如下代码

Yii::setAlias('@api', dirname(dirname(__DIR__)) . '/api');

4 修改 environments/index.php , 添加api 相关的代码

return [
    'Development' => [
        'path' => 'dev',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'frontend/runtime',
            'frontend/web/assets',
            'api/runtime',
            'api/web/assets',
        ],
        'setExecutable' => [
            'yii',
            'yii_test',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
            'api/config/main-local.php',
        ],
    ],
    'Production' => [
        'path' => 'prod',
        'setWritable' => [
            'backend/runtime',
            'backend/web/assets',
            'frontend/runtime',
            'frontend/web/assets',
            'api/runtime',
            'api/web/assets',
        ],
        'setExecutable' => [
            'yii',
        ],
        'setCookieValidationKey' => [
            'backend/config/main-local.php',
            'frontend/config/main-local.php',
            'api/config/main-local.php',
        ],
    ],
];
 

5 然后再执行初始化命令

./init

6 添加虚拟机

添加配置文件 /usr/local/nginx/conf/conf.d/api.baojia.local.conf

server {
    charset utf-8;
    client_max_body_size 128M;
    
    listen 80;
    
    server_name api.baojia.local;
    root        /home/ahcj/www/baojia/api/web/;
    index       index.php;

    access_log   log/api.baojia.local.access.log;
    error_log    log/api.baojia.local.error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # uncomment to avoid processing of calls to non-existing static files 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.conf;
        fastcgi_pass 127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~* /\. {
        deny all;
    }
}

7 在 /etc/hosts 添加域名解析

127.0.0.1   baojia.local 
127.0.0.1   admin.baojia.local 
127.0.0.1   api.baojia.local

7 重载配置文件

[root@localhost conf.d]# ll /usr/local/nginx/conf/conf.d/api.baojia.local.conf 
-rw-r--r-- 1 root root 993 8月  21 10:16 /usr/local/nginx/conf/conf.d/api.baojia.local.conf
[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# service php-fpm reload
Redirecting to /bin/systemctl reload  php-fpm.service

接下来可以使用 api.baojia.local 愉快的访问了

Yii2 Restful 添加 Api 应用_第1张图片
火狐截图_2017-08-21T03-07-27.499Z.png

你可能感兴趣的:(Yii2 Restful 添加 Api 应用)