2018年6月

通常,在Yii2 Controller里做重定向(redirect),一般用下面的方式:

public function actionHello()
{
   return $this->redirect('/world');
}

public function actionWorld()
{
   return 'World!';
}

这种方式的后果是,http status code是302

那么,如果想修改为301怎么办呢?

 return $this->redirect('/world', 301)->send();

感觉很像
是一个key嘛
用于更方便的查询某个tag

暂时这么理解吧 2018.6.14

更新: 20180620
Q: Since Max uses 'ref' as the key to identify a tag, i guess maybe it's similar with the 'id' in html ?

A: Not really. "id" doesn't give you any access to the element - ref does though. With ref, you get a reference that allows you to directly work on the underlying DOM element from within your JS code.

With "id", you could get that access via getElementById and then it would be equivalent but ref doesn't need that extra step.

Behaviors

Behaviors are instances of yii\base\Behavior, or of a child class. Behaviors, also known as mixins, allow you to enhance the functionality of an existing component class without needing to change the class's inheritance. Attaching a behavior to a component "injects" the behavior's methods and properties into the component, making those methods and properties accessible as if they were defined in the component class itself.
Moreover, a behavior can respond to the events triggered by the component, which allows behaviors to also customize the normal code execution of the component.

这两段话,是Yii2官网上对Bahaviors的定义。先尝试着理解第一段话。

- 阅读剩余部分 -

const arr = [10,12,15,21];

/**结果:
 * Index: 4, element: undefined
 * Index: 4, element: undefined
 * Index: 4, element: undefined
 * Index: 4, element: undefined
 */
for (var i = 0; i < arr.length; i++){
    setTimeout(function(){
        console.log('Index: ' + i + ', element: ' + arr[i]);
    }, 3000);
}

/**
 * 下面3个,都是正确结果:
 * Index: 0, element: 10
 * Index: 1, element: 12
 * Index: 2, element: 15
 * Index: 3, element: 21
 */
for (var i = 0; i < arr.length; i++){
    setTimeout(function(i2){
        return function(){
            console.log('Index: ' + i2 + ', element: ' + arr[i2]);
        }
    }(i), 3000);
}

for (let i = 0; i < arr.length; i++){
    setTimeout(function(){
        console.log('Index: ' + i + ', element: ' + arr[i]);
    }, 3000);
}

for (var i = 0; i < arr.length; i++){
    (setTimeout(function(j){
        console.log('Index: ' + j + ', element: ' + arr[j]);
    }, 3000))(i);
}

- 阅读剩余部分 -

yii2-app-basic会经常出现一个bug,就是找不到vendor/bower这个目录,这是由于某种神秘原因,执行composer install的时候,会安装到vendor/bower-asset这个目录下。

具体原因不明。

需要做如下修改:

修改的文件路径是:vendor/yiisoft/yii2/base/Application.php,第461行, 改为bower-asset

- 阅读剩余部分 -

折腾了2个多小时,已经可以用了,虽然还是有点小问题,但已经可以用了,毕竟我的目的不是折腾这个,而是被迫搭建本地HTTPS来测试geolocation api

网上教程很多,尤其是xampp自带的那个makecert,但我本地死活搞不定,各种报错。后来是用cygwin这个神器解决了。

具体情况如下:

大前提:要把php.ini里的openssl选项给开了。当然,肯定还有其他东西也要开,这个就下次再说吧,因为我在设置这个ssl的时候,很多选项已经开了。

首先,如果你写代码调用geolocation这个api,会失败的,因为本地不是https环境,chrome console里给出一个链接。但这个链接里,我只用了2行代码,就是生成ssl证书的代码,在cygwin里生成。

https://mail.mozilla.org/pipermail/web-vr-discuss/2016-July/001272.html

openssl genrsa -out server.key 2048
openssl req -new -x509 -sha256 -key server.key -out server.pem -days 365

注意,这里生成的是server.pem,但是呢,其他教程都用server.crt,这俩就算作是一个意思吧,都是证书。

其次,在apache vhost里设置如下:

<VirtualHost *:80>
    ##ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "D:\xamppwww\www\cardio\public"
    ServerName cardio.com
    ServerAlias www.cardio.com

    RewriteEngine On
    RewriteRule ^/(.*)$ https://cardio.com/$1 [R=301,L]
    ##ErrorLog "logs/dummy-host.example.com-error.log"
    ##CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:443>
    DocumentRoot "D:\xamppwww\www\cardio\public"
    ServerName cardio.com

    SSLEngine on
    SSLCertificateFile "D:\xampp\apache\conf\ssl\server.pem"
    SSLCertificateKeyFile "D:\xampp\apache\conf\ssl\server.key"
</VirtualHost>

这是加了强制跳转的,http强制跳转到https。

第三,要把自签的ssl证书条件到chrome信任列表里,这个要用到chrome settings里的manage certificate这个选项,把server.pem添加到信任列表里,还要在证书目的里再勾选一个客户端验证什么的,总之,就不报错了。
至于chrome仍然显示not secure,那也没懒得管了,因为geolocation api可以成功调用了。

Microsoft Edge浏览器加了就好了没这么多幺蛾子。