Laravel5.7 [TP] write the vender () method to introduce third-party expansion pack sdk

Abstract prospects, new to laravel soon, if not for how the introduction of a third party sdk package, online search is all going to change the composer's automatic loading; do not want to do, I think of the TP method of vender, then try to write about;

Press since before the project has been a habit is to build public functions and constants file documents;

1. Create common.php put Kazakhstan which is not important, find the line in the app catalog;

2. Add in a public / index.php is the project entry file

//引入公共函数,路径要根据自己放哪去写
require __DIR__.'/../app/common.php';

3. Add method common.php years, then all parts of the project can be free to call

//比如个人用习惯了的p方法
/**
 * 打印函数
 * @param array|string $data
 * @param bool $exit
 */
function p($data = [],$exit=false){
    echo '<pre>';
    print_r($data);
    echo '</pre>';
    if(!empty($exit)){
        exit;
    }
}

4. The above is purely Amway, said the following priorities in the root directory (the directory are free, you can find) a new document extends the folder used to store third-party extension kit

5. In the above built in common.php added Vender () method

/**
 * 引入扩展函数 用法:vender('baidu.AipSpeed')或vender('baidu/AipSpeed');
 * @param string $path
 */
function vender($path = ''){
    //允许两种路径表达方式
    $path = str_replace('.','/',$path);
    //若省略文件文件后缀就给补上
    if(!strpos($path,'.php')){
        $path.='.php';
    }
    //这个路径是要看你们自己定的来改
    require_once "../extends/{$path}";
}

6. practical operation:

This is a project directory structure

Sdk third-party call in the controller . If the controller will not be built, the venue https://blog.csdn.net/TXX_c/article/details/82995400

<?php
/**
 * Created by PhpStorm.
 * User: 影TXX
 * Date: 2018/10/22
 * Time: 18:51
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;

class Bd extends Controller
{    
    /**
     * 文字转语音
     */
    function strToMp3(){
        //引入第三方类
        vender('baidu.AipSpeech');
        $speech = new \AipSpeech('146666658','ZHEGESHIJIADEAPIKEY','ZHegEMIyaOYEshIJiAdE');
        $wd = request()->get('wd','TXX真他喵帅');
        $res =  $speech->synthesis($wd,'zh', $ctp=1);
        header('Content-Type:audio/mp3');
        exit($res);
    }

}

 

Published 35 original articles · won praise 18 · views 370 000 +

Guess you like

Origin blog.csdn.net/TXX_c/article/details/83302738