多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
本文介绍一下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 <?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 <?php namespace app\common\dao\test; use app\common\dao\BaseDao; use app\common\model\test\IsTest; class IsTestDao extends BaseDao { protected function getModel(): string { return IsTest::class; } } ``` 3. 创建repoository文件 路径:app/common/repoository/test/IsTestRepoository.php ```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目录,用户创建在 api 目录 路径:app/conotroller/admin/test/IsTest.php ```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; } } ``` 这样我们的必备的几个基础文件就好了,以上每个文件中的方法,都是必须创建的,否则会报错。 - controller主要是针对路由对外访问的接口方法 - repoository就是写一些公用的会重复利用的逻辑处理等方法 - dao针对数据库的操作 - model定义数据表映射对象 ### 三.创建新的接口,开发功能 1. 因为是平台功能,就在route/admin.php文件增加路由,修改路由文件后记得重启一下swoole服务。 ```php Route::group('is_test',function(){ Route::post('create', '/create')->name('systemIsTestCreate'); })->prefix('admin.test.IsTest); ``` 2. 在controller文件中写相对应的功能,创建方法create ```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; } public function create() { $data = $this->request->params(['name']); $this->repository->create($data); return app('json')->success('添加成功'); } } ``` 这样我们的一个添加数据的功能就完成了,当然如果有更多数据和逻辑需要处理,就可以在IsTestRepository 这个文件中创建一个create()方法,然后做想相对应的处理,比如把name存储为json字符串 ```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; } public function create($data) { $data = [ 'name' => json_encode($data) ]; $this->dao->create($data); } } ``` 如果需要调用别的控制器的方法可以是用make方法,例如想在添加的时候调用user表查看数据 ```php <?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); } } ```