第一步:先在app目录下新建目录app/libs/express(App名字)/, 用于存放各种应用程序,
比如:app/libs/express/DadaExpress.php,代码如下:
<?php
use WeiZhi\Service\DadaExpressInterface;
class DadaExpress implements DadaExpressInterface
{
public function __construct($url, $appKey)
{
}
public function test()
{
return 'ok------------';
}
}
?>
第二步:在app目录下新建目录app/WeiZhi/Service/,用于存放各种应用的服务接口文件:比如:\app\WeiZhi\Service\DadaExpressInterface.php
<?php
namespace WeiZhi\Service;
interface DadaExpressInterface
{
public function test ();
}
?>
第三步:然后在composer.json中把以上创建的应用目录添加进去:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/database/migrations",
"app/database/seeds",
"app/libs"
],
"psr-0": {
"WeiZhi": "app"
}
},
App的命名空间随意定,执行下composer dump。(已执行过,不用执行了)
第四步:在app/start/global.php的文件中添加
App::bind('WeiZhi\Service\DadaExpressInterface', function($app)
{
return new DadaExpress('','');
});
第五步:在\vendor\composer\autoload_classmap.php文件的数据中添加以下2行
1. 'WeiZhi\\Service\\DadaExpressInterface' => $baseDir . '/app/WeiZhi/Service/DadaExpressInterface.php',
2. 'DadaExpress' => $baseDir . '/app/libs/express/DadaExpress.php',
第六步:创建controller调用服务
比如:\app\controllers\DadaController.php,代码如下:
<?php
use WeiZhi\Service\DadaExpressInterface;
class DadaController extends BaseController
{
protected $dadaExpress;
public function __construct(DadaExpressInterface $dadaExpress)
{
$this->dadaExpress = $dadaExpress;
}
public function testdemo()
{
echo $this->dadaExpress->test();
}
}
(原创:邵珠民)