Config static config()


  • use
  • bind
    静态方法,全局配置QueryList,返回值为QL\Config对象.

QL\Config 对象


方法列表:

  • use() 全局安装插件
  • bind() 全局功能扩展

Config use($plugins,…$opt)

用法同QueryList的use方法,全局安装插件后,所有QueryList对象均可使用这些插件。

  1. QueryList::config()->use(My\MyPlugin::class,$arg1,$arg2,$arg3);
  2. QueryList::config()->use(My\MyPlugin::class)->use([
  3. My\MyPlugin::class,
  4. My\MyPlugin2::class,
  5. Other\OtherPlugin::class
  6. ]);



Config bind(string $name, Closure $provider)

用法同QueryList的bind方法,全局功能扩展,所有QueryList对象均可使用扩展的方法。

  • 例一
  1. //全局注册一个自定义的编码转换方法
  2. QueryList::config()->bind('myEncode',function($outputEncoding,$inputEncoding){
  3. $html = iconv($inputEncoding,$outputEncoding.'//IGNORE',$this->getHtml());
  4. $this->setHtml($html);
  5. return $this;
  6. });
  7. $data = QueryList::get('https://top.etao.com')->myEncode('UTF-8','GBK')->find('a')->texts();
  8. print_r($data->all());
  • 例二
  1. //全局注册一个myHttp方法到QueryList对象
  2. QueryList::config()->bind('myHttp',function($url){
  3. $html = file_get_contents($url);
  4. $this->setHtml($html);
  5. return $this;
  6. });
  7. $data = QueryList::myHttp('https://top.etao.com')->find('a')->texts();
  8. print_r($data->all());