• 学习笔记-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

  • 相关阅读:
    [Linux入门]---搭建Linux环境
    编写一个vscode的插件
    WordPress还原重置插件WP Reset 教程!
    clickhouse停止和避免mutations操作
    如何开发LAXCUS分布式应用软件(一)
    AI作诗,模仿周杰伦创作歌词<->实战项目
    RocketMQ 之 IoT 消息解析:物联网需要什么样的消息技术?
    Elasticsearch - 文档分析,IK分词器;文档冲突(十二)
    好用的Java工具类库—— Hutool
    VUE快速入门-2
  • 原文地址:https://blog.csdn.net/sinat_35360663/article/details/127775211