fly020212

fly020212

这家伙有点懒,还没写个性签名!

  • 财富值342
  • 威望值100
  • 总积分1582

个人信息

  • 回复了 的回答

    main是主要渲染的layout,所以在每个视图几乎都会渲染到这个layout,那么你的方法就必须要能够让每个控制器都能继承到(或者能调用到),那么你可以在Controller定义一个方法,那么你在layout那里就可以用 $this->function(); 来获取你要的值了

    layout 里面,$this 指向的对象是 View ,需要用 $this->context 才能引用到 Controller

  • findAll 也是间接的调用了 find()..all()

  • find() 返回的是一个 queryBuilder, 可以 select,where, andWhere, group, orderBy,offset, limit 等,findAll() 可没有这些

  • 你给的例子,其实不能完全说明 behaviors 的行为,这里是 filter 的行为,但 filteryii2yii 中的实现不一样,yii2 中的 filter 是特殊的 behavior ,被框架多加了一层的处理,这里我只说明下 filter 的调用, 普通的 behavior 我暂时还没有用到,不清楚

    一句话,QueryParamAuthauthenticator() 方法,是在 Controller 调用 beforeAction 时触发的,你可以在 Controller 复写 beforeAction 方法为空操作,看看还会不会调用。

    在底层的 Controller 中,beforeAction 执行了 $this->trigger(self::EVENT_BEFORE_ACTION, $event) 触发了 EVENT_BEFORE_ACTION 事件,在 yii\base\Componenttrigger 会首先 ensureBehaviors(),把 behaviors() 定义的行为附加到 Controller

    /**
     * Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
     */
    public function ensureBehaviors()
    {
        if ($this->_behaviors === null) {
            $this->_behaviors = [];
            foreach ($this->behaviors() as $name => $behavior) {
                $this->attachBehaviorInternal($name, $behavior);
            }
        }
    }
    /**
     * Attaches a behavior to this component.
     * @param string|integer $name the name of the behavior. If this is an integer, it means the behavior
     * is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name
     * will be detached first.
     * @param string|array|Behavior $behavior the behavior to be attached
     * @return Behavior the attached behavior.
     */
    private function attachBehaviorInternal($name, $behavior)
    {
        if (!($behavior instanceof Behavior)) {
            $behavior = Yii::createObject($behavior);
        }
        if (is_int($name)) {
            $behavior->attach($this);
            $this->_behaviors[] = $behavior;
        } else {
            if (isset($this->_behaviors[$name])) {
                $this->_behaviors[$name]->detach();
            }
            $behavior->attach($this);
            $this->_behaviors[$name] = $behavior;
        }
        return $behavior;
    }
    

    }

    QueryParamAuth 最终继承自 ActionFilter 的,来看看 ActionFilterattach 时会做什么

    /**
     * @inheritdoc
     */
    public function attach($owner)
    {
        $this->owner = $owner;
        $owner->on(Controller::EVENT_BEFORE_ACTION, [$this, 'beforeFilter']);
    }
    

    会在 Controller 上绑定一个 EVENT_BEFORE_ACTION 事件,行为是执行自己的 beforeFilter 方法

    /**
     * @param ActionEvent $event
     */
    public function beforeFilter($event)
    {
        if (!$this->isActive($event->action)) {
            return;
        }
    
        $event->isValid = $this->beforeAction($event->action);
        if ($event->isValid) {
            // call afterFilter only if beforeFilter succeeds
            // beforeFilter and afterFilter should be properly nested
            $this->owner->on(Controller::EVENT_AFTER_ACTION, [$this, 'afterFilter'], null, false);
        } else {
            $event->handled = true;
        }
    }
    

    到这里,你看到了,ActionFilter 最终会执行自己的 beforeAction 方法,当然这可不是 Controller 中的 beforeAction 方法,还会处理些前置check,如 exceptonly 检测,这个 beforeAction 正是你写 filter 时需要复写的一个方法,至于 QueryParamAuthauthenticator() 方法,自己可以接着走代码

    入口是 ControllerbeforeAction 方法,可以自己走一遍源代码,就这些

  • 76657fa42121a296eb44cb28fe07d43c8a69d011s:10:"1234567890";

    76657fa42121a296eb44cb28fe07d43c8a69d011 这个看起来像 sha1 的结果

    s:10:"1234567890" 这个是 php 序列化的结果,感觉你 a 和 b 上读取的时候代码不一致,或者 cookie 的配置不一样,把两段读取cookie 的代码贴出来看下,

  • rules() 方法里面设置默认值,一个必要的条件是,需要调用 $model->validate() 方法,不管是直接调用,还是通过 $model->save() 方法间接调用,在展示 新增界面 的时候使用不是很合适,有一个比合适的方法 $model->loadDefautlValues() ,在新增界面,new model 之后,调用 loadDefaultValues 方法,将数据库的默认值设置过来

  • 好像有个方法是 $this->renderAjax() 的,不会渲染 layout,但是会走 assets 的,具体可以看下 API 和源代码

  • save 方法的定义

    boolean save( $runValidation = true, $attributeNames = null )
    
  • 这个, API 的注释已经相当清楚了,true 验证数据, 会调用 validate() 方法,false 不验证数据,直接存储,至于是更新数据,还是插入数据,就看 $this->getIsNewRecord() 的返回结果了,跟 save 方法的参数没有丁点关系

  • 估计楼主的意思是

    return $this->render("<html><?php echo 'test';?></html>");
    

    yii2 貌似是没有这种调用方式的,我估计以后也不会有

    \yii\base\View 有一个方法是可以渲染 php 代码的,是通过 eval 执行的,可以这样调用

    $this->getView()->renderDynamic("echo 'text'");
    

    不过还是尽量不要直接渲染 php 代码吧

经理 等级规则
1582/2000
资料完整度
10/100
用户活跃度
0/100

Ta的关注

1

Ta的粉丝

8

Ta的访客

25