如何在模块内引入第三方类库
模块内引入第三方类库有2种方式:
add_namespace
添加名字空间,如:Cron\Crontab
类库位于schedule
模块library
目录下,在HttpRun
事件中添加以下代码:public function onHttpRun()
{
add_namespace([
'Cron' => 'schedule' . DS . 'library' . DS . 'Cron' . DS
]);
}
- 导入
composer
包,例如导入微信开发包easywechat
,在模块根目录下执行composer require overtrue/wechat
。添加导入事件
在需要使用前调用public function onLoadEasyWechat($payload)
{
if (!class_exists(\EasyWeChat\Factory::class)) {
require_once app_path() . DS . 'third' . DS . 'vendor' . DS . 'autoload.php';
}
}
ev('LoadEasyWechat')
即可。
<p>模块内引入第三方类库有2种方式:</p>
<ol>
<li><code>add_namespace</code> 添加名字空间,如:<br><code>Cron\Crontab</code> 类库位于 <code>schedule</code> 模块 <code>library</code> 目录下,在 <code>HttpRun</code> 事件中添加以下代码: <pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> onHttpRun</span><span class="pun">()</span></code></li><li class="L1"><code><span class="pln"> </span><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> add_namespace</span><span class="pun">([</span></code></li><li class="L3"><code><span class="pln"> </span><span class="str">'Cron'</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="str">'schedule'</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS </span><span class="pun">.</span><span class="pln"> </span><span class="str">'library'</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS </span><span class="pun">.</span><span class="pln"> </span><span class="str">'Cron'</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">]);</span></code></li><li class="L5"><code><span class="pln"> </span><span class="pun">}</span></code></li></ol></pre></li><li>导入 <code>composer</code> 包,例如导入微信开发包 <code>easywechat</code>,在模块根目录下执行 <code>composer require overtrue/wechat</code> 。添加导入事件<pre class="prettyprint linenums prettyprinted" style=""><ol class="linenums"><li class="L0"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">function</span><span class="pln"> onLoadEasyWechat</span><span class="pun">(</span><span class="pln">$payload</span><span class="pun">)</span></code></li><li class="L1"><code><span class="pun">{</span></code></li><li class="L2"><code><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(!</span><span class="pln">class_exists</span><span class="pun">(</span><span class="pln">\EasyWeChat\Factory</span><span class="pun">::</span><span class="kwd">class</span><span class="pun">))</span><span class="pln"> </span><span class="pun">{</span></code></li><li class="L3"><code><span class="pln"> require_once app_path</span><span class="pun">()</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS </span><span class="pun">.</span><span class="pln"> </span><span class="str">'third'</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS </span><span class="pun">.</span><span class="pln"> </span><span class="str">'vendor'</span><span class="pln"> </span><span class="pun">.</span><span class="pln"> DS </span><span class="pun">.</span><span class="pln"> </span><span class="str">'autoload.php'</span><span class="pun">;</span></code></li><li class="L4"><code><span class="pln"> </span><span class="pun">}</span></code></li><li class="L5"><code><span class="pun">}</span></code></li></ol></pre>在需要使用前调用 <code>ev('LoadEasyWechat')</code> 即可。</li></ol>