• 学习笔记-TP5框架学习笔记进阶之Contorller


    很烂,不建议看。建议略过 ## Controller

    大多是一些模板渲染的方法。从构造函数中就可以看出来,

    public function __construct(Request $request = null)
    {
    $this->view = View::instance(Config::get('template'), Config::get('view_replace_str'));
    $this->request = is_null($request) ? Request::instance() : $request;
    // 控制器初始化
    $this->_initialize();
    // 前置操作方法
    if ($this->beforeActionList) {
    foreach ($this->beforeActionList as $method => $options) {
    is_numeric($method) ?
    $this->beforeAction($options) :
    $this->beforeAction($method, $options);
    }
    }
    }

    刚开始就实例化了View类。想到之前复现的ThinkPHP的SQL注入漏洞,有些测试代码就会调用fetch、assign、display这些方法。而模块开发的开始就是从 application下面开始的。应用模块的控制器如果继承了Controller类,就能直接调用这些方法进行视图渲染。

    这个类还有两个方法:beforeAction和validate

    beforeAction是前置操作:设置 beforeActionList属性可以指定某个方法为其他方法的前置操作,数组键名为需要调用的前置方法名,无值的话为当前控制器下所有方法的前置方法。

    ['except' => '方法名,方法名']
    #表示这些方法不使用前置方法,
    ['only' => '方法名,方法名']
    #表示只有这些方法使用前置方法。
    # demo
    protected $beforeActionList = [
    'first',
    'second' => ['except'=>'hello'],
    'three' => ['only'=>'hello,data'],
    ];
    # 类内所有方法调用前都会调用first方法
    # 类内hello方法不会调用second这个前置方法
    # 只有hello,data方法会前置调用three方法。

    如果你在自己继承了Controller的控制器代码中定义了protected $beforeActionList,那么它就会在构造函数中foreach处理后,调用beforeAction进行only还是except的判断,最后用call_user_func调用。

    call_user_func([$this, $method])

    这种写法是对类内方法的调用,这里的$this指向的是继承Controller类的Index类。也就是说回调的函数必须在Index类中定义好了。

    validate验证器方法主要还是调用的Validate类,应该是为了方便控制器对数据的验证。

    具体流程就是用 加载验证器 的 加载器,来加载验证器。。。。。

    如果传入的$validate是个数组,就先用thinkphp\library\think\Validate.php#rule赋到$this里面。不是数组的话就直接调用。

    点击关注,共同学习!
    [安全狗的自我修养](https://mp.weixin.qq.com/s/E6Kp0fd7_I3VY5dOGtlD4w)


    [github haidragon](https://github.com/haidragon)


    https://github.com/haidragon

  • 相关阅读:
    (附源码)计算机毕业设计SSM基于的汉服服装租赁系统
    华为云,阿里云,腾讯云 安全组配置规则
    iOS Swift 拍照识别数字(Recognizing Text in Images)
    接收灵敏度和等效噪声带宽(ENBW)
    战略调整?顺丰科技将从深圳撤退到武汉!
    ChatGPT企业版来了,速度翻倍,无使用限制
    Flink中常用的去重方案
    【数字化】分享-广东省企业首席数据官建设指南
    测试设计中隐藏的边界有哪些?
    微软成AI热潮大赢家,继续押注大模型和人工智能
  • 原文地址:https://blog.csdn.net/sinat_35360663/article/details/127775211