蓝
- 蓝 回答了问题 RESTful 速率限制问题
从我贴的那个链接中 截两段话
图1:
认证状态不能保持,每一次请求都是通过 access token 实时认证的图2:
认证成功后 才会进行频率限制,所以 RateLimit 类的这个方法中public function beforeAction($action) { if ($this->user === null && Yii::$app->getUser()) { $this->user = Yii::$app->getUser()->getIdentity(false); } if ($this->user instanceof RateLimitInterface) { Yii::debug('Check rate limit', __METHOD__); $this->checkRateLimit($this->user, $this->request, $this->response, $action); } elseif ($this->user) { Yii::info('Rate limit skipped: "user" does not implement RateLimitInterface.', __METHOD__); } else { Yii::info('Rate limit skipped: user not logged in.', __METHOD__); } return true; }
并不会进入第一个 if 判断
- 蓝 回答了问题 RESTful 速率限制问题
既然都 restful 了,肯定不应该在使用 基于 session 的用户认证模式了,建议你看下权威指南的这一章
https://www.yiiframework.com/doc/guide/2.0/zh-cn/rest-authentication - 蓝 回答了问题 请问一下 Yii 2.0 多条件筛选路由如何设置呢?
几个方案吧:
1、如果你的参数都是必不可少的,且参数顺序固定,那 很简单'warehouse/c_<c>/big_<big>/s_<s>/w_<w>/a_<a>/b_<b>' => 'warehouse/index'
2、如果你的参数可选,或顺序不定,简单处理的话 就是把他们都弄到一个变量里,自己在程序中拆分这些变量
'warehouse/<params:\S*>' => 'warehouse/index'
params 这个参数得到的是 c_2/big_1/s_14/w_20/a_12/b_13 这样一个字符串,自己拆分处理
3、讲究点的做法,自己写一个 专门处理这种 URL 的类,比如叫
MyUrlRule
,实现yii\web\UrlRuleInterface
这个接口,实现其中的parseRequest
和createUrl
两个方法,基本上也就是 拆分 和 拼接 的操作,代码就不在这里献丑了,然后'urlManager' => [ ... 'rules' => [ ... ['class' => 'app\components\MyUrlRule'], ... ] ]
- 蓝 回答了问题 Yii2 如何输出标准的 JS?
更 yii 一点的做法是,仿照
yii\web\HtmlResponseFormatter
或yii\web\JsonResponseFormatter
或yii\web\XmlResponseFormatter
,自己写一个JavascriptResponseFormatter
类(这个类唯一的作用 也就是设置Content-Type
),然后就可以这样Yii::$app->response->format = Response::FORMAT_JAVASCRIPT; Yii::$app->response->data = "document.write('<a href=\"https://test.com/test.php\">test string</a>');"; return Yii::$app->response->send();
当然
FORMAT_JAVASCRIPT
这个常量的定义 还需要修改 Response 类,并且加上 到自己写的那个类的映射 - 蓝 回答了问题 Yii2 如何输出标准的 JS?
简单点的话 直接 echo 就行
讲究点的话,可以这样:// 没有这行的话,下面的设置 content-type 不好使,因为 默认的 format 是 text/html,send 的时候会用默认的 format 把 content-type 覆盖掉 Yii::$app->response->format = Response::FORMAT_RAW; Yii::$app->response->headers->set('Content-Type', 'application/javascript'); Yii::$app->response->data = "document.write('<a href=\"https://test.com/test.php\">test string</a>');"; return Yii::$app->response->send();
- 蓝 回答了问题 Yii2 查询构造器问题
具体怎么个“不行”法?这样写是 没问题的
当然,$rows 这个变量的值 有变化了,弄个其它的变量吧$a = (new \yii\db\Query())->select(['id', 'email'])->from('user'); $rows = $a->where(['last_name' => 'Smith'])->limit(10)->all();
当然 这样也行
$rows = (new \yii\db\Query())->select(['id', 'email'])->from('user'); $rows = $rows->where(['last_name' => 'Smith'])->limit(10)->all();
- 蓝 回答了问题 请问一下 HTTP 503 如何重定向?
DNS 里配个 cname
正确的做法是 直接把 apache 的 document_root 配置到 frontend/web