🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 创建helpers.php ![](https://img.kancloud.cn/2d/ea/2deaa9004e54abebc1fe15bdeda19f24_878x252.png) ## index.php调用 ![](https://img.kancloud.cn/b6/52/b6521ab6554b10f44288565fa4183633_622x324.png) ## 不规范 直接 `include` 虽然可以,但是没有遵守规范。 `composer` 提供了 `include` 文件方式。 ## 遵守规范 编辑 `composer.json`。 ``` "files": [ "app/helpers.php" ] ``` ![](https://img.kancloud.cn/1f/18/1f18fa5ad63dbeaad5c594848d06acb4_975x456.png) 执行 `compoer dump-autoload` ## index.php调用 ![](https://img.kancloud.cn/40/7d/407dc60427be2602a58c9d3c846077f0_666x269.png) ## 补充 将这些函数添加到helpers.php,方便以后写代码。 ``` <?php function hello() { return "world"; } if (! function_exists('response')) { function response() { return App::getContainer()->get('response') return App::getContainer()->getApp('response'); // 这是错误的 } } function app($name = null) { if( $name) // 如果选择了具体实例 return App::getContainer()->get($name); return App::getContainer(); } function endView() { $time = microtime(true) - FRAME_START_TIME; $memory = memory_get_usage() - FRAME_START_MEMORY; echo '<br/><br/><br/><br/><br/><hr/>'; echo "运行时间: ". round($time * 1000,2) .'ms<br/>'; echo "消耗内存: ". round($memory / 1024 / 1024,2) . 'm'; } function config($key = null) { if( $key) return App::getContainer()->get('config')->get($key) ; return App::getContainer()->get('config'); } ```