本文介绍一下CRMEB多商户二次开发的操作流程,从创立数据库,到实现一个残缺增加数据的过程,其余更多办法实现只是路由和办法名的差别,也就不过多赘述。
一、创立数据库
例如数据库名为:eb_is_test
字段为:id,name
SQL:
CREATE TABLE eb_is_test
(id
int(11) unsigned NOT NULL AUTO_INCREMENT,name
varchar(111) DEFAULT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
二、创立必要文件,为了更好的治理我给这个模块独自减少一个test文件目录。
1.首创立model :
门路:app/common/moel/test/IsTest.php
代码如下:
<?php
namespace app\common\model\test;
use app\common\model\BaseModel;
class IsTest extends BaseModel
{
public static function tablePk(): ?string{ return 'id';}public static function tableName(): string{ return 'is_test';}
}
2.创立dao文件:
门路 :app/common/dao/test/IsTestDao.php
代码如下:
<?php
namespace app\common\dao\test;
use app\common\dao\BaseDao;
use app\common\model\test\IsTest;
class IsTestDao extends BaseDao
{
/** * @return string * @author Qinii */protected function getModel(): string{ return IsTest::class;}
}
3.创立repoository文件
门路:app/common/repoository/test/IsTestRepoository.php
代码如下:
<?php
namespace app\common\repositories\test;
use app\common\dao\test\IsTestDao;
use app\common\repositories\BaseRepository;
class IsTestRepository extends BaseRepository
{
protected $dao;public function __construct(IsTestDao $dao){ $this->dao = $dao;}
}
4.创立contorller,咱们创立平台的控制器吧(平台后盾的操作那就在admin,商户就是merchant)。
门路:app/conotroller/admin/test/IsTest.php
代码如下:
<?php
namespace app\controller\admin\test;
use app\common\repositories\test\IsTestRepository;
use crmeb\basic\BaseController;
use think\App;
class IsTest extends BaseController
{
protected $repository;public function __construct(App $app,IsTestRepository $repository){ parent::__construct($app); $this->repository = $repository;}
}
这样咱们的必备的几个根底文件就好了,以上每个文件中的办法,都是必须创立的,否则会报错。
三、减少路由,开发性能。
1.因为是平台性能,就在route/admin.php文件减少路由,批改路由文件后记得重启一下swoole服务。
Route::group('is_test',function(){
Route::post('create', '/create')->name('systemIsTestCreate');
})->prefix('admin.test.IsTest);
2.在controller文件中写绝对应的性能,创立办法create,如下:
<?php
namespace app\controller\admin\test;
use app\common\repositories\test\IsTestRepository;
use crmeb\basic\BaseController;
use think\App;
class IsTest extends BaseController
{
protected $repository;public function __construct(App $app,IsTestRepository $repository){ parent::__construct($app); $this->repository = $repository;}public function create(){ $data = $this->request->params(['name']); $this->repository->create($data); return app('json')->success('增加胜利');}
}
这样咱们的一个增加数据的性能就实现了,当然如果有更多数据和逻辑须要解决,就能够在IsTestRepository 这个文件中创立一个create()办法,而后做想绝对应的解决,比方把name存储为json字符串,代码如下:
<?php
namespace app\common\repositories\test;
use app\common\dao\test\IsTestDao;
use app\common\repositories\BaseRepository;
class IsTestRepository extends BaseRepository
{
protected $dao;public function __construct(IsTestDao $dao){ $this->dao = $dao;}public function create($data){ $data = [ 'name' => json_encode($data) ]; $this->dao->create($data);}
}
以上就是一个残缺的二开流程,如果须要调用别的控制器的办法能够是用make办法,例如想在增加的时候调用user表查看数据:
<?php
namespace app\common\repositories\test;
use app\common\dao\test\IsTestDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\user\UserRepository;
class IsTestRepository extends BaseRepository
{
protected $dao;public function __construct(IsTestDao $dao){ $this->dao = $dao;}public function create($data){ //$user = app()->make(UserRepository::class)->get(1); //此处办法和下面一行的写法统一,只是这样写能够不必反复make $make = app()->make(UserRepository::class); $user = $make->get(1); $data = [ 'name' => json_encode($data) ]; $this->dao->create($data);}
}
controller次要是针对路由对外拜访的接口办法,repoository就是写一些专用的会反复利用的逻辑解决等办法,dao针对数据库的操作。
如果你感觉这篇文章对你有点用的话,麻烦请给咱们的开源我的项目点点star: http://github.crmeb.net/u/defu 不胜感激 !