Yii: requiring a .php file

2019-06-16 10:42发布

I develop a project with Yii.

I need to require a plain .php file (not component, not a class, just a regular sequence of PHP functions definitions). What is the correct way to do this under Yii framework? Should I use plain require_once()?

require_once(Yii::app()->basePath . '/extensions/my-php-file.php');

Right?

标签: php yii
2条回答
【Aperson】
2楼-- · 2019-06-16 11:01

Yes, that's correct.

Example:

require_once Yii::app()->basePath . '/extensions/PearMail/Mail-1.2.0/Mail.php';
查看更多
Evening l夕情丶
3楼-- · 2019-06-16 11:14

Method in your question is fine, but forces file location to be always in same folder.

Flexible Yii::import is intented for loading classes only, and only classes whose name is same as filename.

However if you want to use aliases to get path to file, you can still benefit from aliases by using getPathOfAlias, like that:

require_once Yii::getPathOfAlias('application.extensions.my-php-file') . '.php';

To include file relative to current file it is good to use dirname(__FILE__) or __DIR__ (php 5.3+)

require_once __DIR__ . '/some-file.php';
查看更多
登录 后发表回答