search
Homephp教程php手册yii 自动加载其他类

Yii2在项目中使用了composer来管理第三方的类库,Yii呢?就没有这样的目录了。不过在他自己的目录结构里其实有这样目录结构,比如extensions。而且默认是用Yii::import(ext.xxxxx.*);来加载 如果再建一个vendor的目录呢?如果我也要用composer怎么办 呢?Yii

Yii2在项目中使用了composer来管理第三方的类库,Yii呢?就没有这样的目录了。不过在他自己的目录结构里其实有这样目录结构,比如extensions。而且默认是用Yii::import("ext.xxxxx.*");来加载

如果再建一个vendor的目录呢?如果我也要用composer怎么办 呢?Yii的加载是靠类名自动加载的。而它的类名却没有办法认到Vendor目录下的程序。这时候应该怎么办呢?

所以Yii还提供了一个简单的办法:Yii::registerAutoloader(array('Requests', 'autoloader'));

看到这个Requests了没,这个就是第三方的HTTP类库,官网是:http://requests.ryanmccue.info/,这应该是目前PHP中最好的Request的类库了,但默认,它的autoload方式,Yii不支持,所以在Requests.php的第一行加入上面的代码。这回好了,Yii::import("ext.Requests"); 你就可以在任何地方调用:Requests::get($url),而不用担心找不到类了

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools