”Yii2高级版本创建新的应用主体“及“创建restful API的过程”

一、复制backend至api,  environments/dev/backend至environments/dev/api以及environments/prod/backend至environments/prod/api.

二、修改配置文件api/config/main.php,需要修改应用id、命名空间、用户组件和url美化的配置内容
'id' => 'app-api',
'controllerNamespace' => 'api\controllers',

等,直接批量替换backedn为api

三、在common\config\bootstrap.php中添加api的路径别名
Yii::setAlias('@api', dirname(dirname(__DIR__)).'/api');

其它需要操作的步骤,:

1、执行根目录下init.bat脚本,需要在CMD中到对应的目录,直接输入init.bat,然后输入0,y

2、修改api文件中,controllers,models,assets,views中文件的backend为api。

3、我的是PHPstudy。目录设置为:D:\wwwroot\phproot\p7\api\web,hosts这样设置:

192.168.2.1**  api.p7.cc
192.168.2.1**  frontend.p7.cc
192.168.2.1**  backend.p7.cc

4、如果没有给php.exe添加环境变量,还需要添加。

https://jingyan.baidu.com/article/47a29f24610740c0142399ea.html

https://jingyan.baidu.com/album/f00622283dd0c5fbd3f0c893.html?picindex=5

5、提示:the openssl php extension is required by yii2

A、httpd.conf的LoadModule ssl_module modules/mod_ssl.so要打开。

B、php.ini的extension=php_openssl.dll的注释打开

6、2019.2.9 时下载的yii2高级版在win10的电脑上、phpstudy 需要切换到php5.6以上才配置成功。

7、在执行init.bat之后,frontend及backend都自动设置了cookieValidationKey,但是新模块api需要单独配置:cookieValidationKey

测试结果:

http://backend.p7.cc:8712/  可以打开

http://frontend.p7.cc:87/ 可以打开

http://api.p7.cc:8711/site/login 经过以上以下操作都打不开,跳转到这个网址,检查原因,猜想是第2小点没操作(后经证实是win10+phpstudy的伪静态不能设置成功)

这种模式全成功:

http://www.p9.cc:89/api/web/index.php?r=site%2Flogin

http://www.p9.cc:89/frontend/web/index.php?r=site%2Flogin

http://www.p9.cc:89/backend/web/index.php?r=site%2Flogin

3、修改 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',
        ],
    ],
];

 

创建restful API的过程

一、创建了article控制器,复制sitecontroller.php改为articlecontroller.php

<?php
namespace api\controllers;


use yii\web\Controller;


/**
 * Site controller
 */
class ArticleController extends Controller
{
    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {
        //return $this->render('index');
        die("ok");
    }

}


二、在配置文件中,创建了针对article控制器的url美化规则

'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
                ['class'=>'yii\rest\UrlRule', 'controller'=>'article'],
            ],
        ],


三、配置main-local文件,让API应用能接收JSON格式的输入数据(要在init.bat之后才有这个文件),yii 配置接受和返回json       路径: api\config\main-local.php


'components' => [
        'request' => [
            'csrfParam' => '_csrf-app',
            'parsers' => [
                'text/xml' => 'common\components\XmlRequestParser',
                'application/xml' => 'common\components\XmlRequestParser',
                'application/json' => 'yii\web\JsonParser',
                'text/json' => 'yii\web\JsonParser',
            ],
        ],

]
'components' => [
        // ...
        'response' => [
            "format" => \yii\web\Response::FORMAT_JSON;
            'as format'=>api\behaviors\ResponseFormatBehavior::className()
        ],

],

四 其它操作

1、在win10、phpstudy2018的环境下,yii2隐藏index.php的测试没有成功。

2、在远程linux服务器上配置yii2高级版出现以下问题,open_basedir restriction in effect:https://www.jianshu.com/p/64ccaa004fb2

https://www.cnblogs.com/feixiablog/articles/9667852.html

3、宝塔伪静态(Apache)在:  网站->设置->伪静态里面添加:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

4、注意,在做restful API的一系列操作之后,是不能按正常方式访问url的。

5、参考网址:

https://www.cnblogs.com/xd502djj/p/7137466.html

https://www.yiichina.com/tutorial/1606

https://www.cnblogs.com/peteremperor/p/6791959.html

http://blog.51cto.com/13238147/2148828

https://www.cnblogs.com/peteremperor/p/6791959.html

6、在解决问题的过程中,多次因为不细心而造成代码错误,所以按步骤复查也是发现问题的重要事项。

7、1001种试错:一个框架,没有时间去了解各种底层,只能是根据自己所掌握的知识及网上的教程来分析问题。有这样一个问题难住了我,restful API在POST数据时,发现只有ID保存成功了,其它的数据都没有保存成功,不知是啥问题,网上也没有教程,只是一些copy的。我试了以下各种猜想,最终找到了答案,只是这个速度好慢:

A、环境系统不兼容?我又在本地机上测试了,发现还是同样的问题

B、是yii2的高级版与基础版有区别?

C、根据网上的教程有哪个细节错了?

D、忽然想起,yii2有一个数据模型的rule机置,将需要post的数据设置为safe,成功post新建数据成功。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值