分享
  • 收藏
  • 举报
    X
    简单的模块制作说明---进阶篇(更新中)
    957
    7

    上期说了最简单最原始的模块制作 哪个方法是简单方便快捷,弊端就是不够个性化因为都是继承了 C M F S等我们不方便修改那么这期咱们就说说怎么个性化不用去集成那些类去做模块。

    以下教程起码有一定的基础才能看得懂 文字会很少代码量比较大,看不懂的就不要看了看懂的点个赞就好。

    以搜索模块为例:

    Info.php

    <?php
    namespace app\search; //命名空间到 search 目录
    class Info{
    	public static $keyword;    //关键字,也是目录名
    }

    config.php

    <?php
    return [
    //注意到没 这里就仅仅一行就足够了 不需要的配置项之间咔嚓掉就可以了 更权威的等待官方文档 
    	'system_dirname'    =>basename(__DIR__),
    ];

    admin_menu.php 和常规模块配置一样的方法就不注释了 请看上一篇文章

    <?php
    return [
    	'search'=>[
    		'title'=>'search',
    		'sons' =>[
    			[
    				'title'=>'功能设置',
    				'sons' =>[
    					[
    						'title'=>'参数设置',
    						'link' =>'setting/index',
    						 
    					],
    					 [
    						'title'=>'热搜词管理',
    						'link' =>'content/index',
    						 
    					],
    				],
    			],
    		],
    	],
    ];

    admin目录

    Setting.php

    <?php
    namespace app\search\admin; 
    use app\common\controller\admin\Setting AS _Setting; //导入app\common\controller\admin\Setting 命名为 _Setting
    //继承_Setting
    class Setting extends _Setting{
    	/**
    	 * 参数设置
    	 * {@inheritDoc}
    	 * @see \app\common\controller\admin\Setting::index()
    	 调用父类的index方法
    	 */
    	public function index($group=null){
    		return parent::index($group);
    	}
    }

    熟悉TP的人会问 这里应该是 return $this->fetch(); 才对 但是我可以很高兴的告诉你 这里吧需要 只要调用 

    parent::index

    即可 他会输出模板 读取你的配置 配置参数下面会讲 当然了你要想自己输出一个模板也是可以的 创建对应的文件夹和文件即可

    Content.php

    <?php
    namespace app\search\admin;
    use app\common\controller\AdminBase;
    use app\search\model\Content AS Model;
    use app\common\traits\AddEditList;
    class Content extends AdminBase{
    	use AddEditList;
    	protected $model;
    	protected $form_items=[];
    	protected $list_items;
    	protected $tab_ext;
    
    	protected function _initialize(){
    		parent::_initialize();
    		$this->model=new Model();
    		$this->form_items=[['text','keyword','关键词'],['text','searchnums','搜索次数'],];
    	}
    
    	/**
    	 * 首页
    	 * @return mixed|string
    	 */
    	public function index(){
    		$this->tab_ext=['page_title'=>'热搜管理',];
    		$this->list_items=[['keyword','关键词名称','text.edit'],['searchnums','搜索次数','text.edit'],];
    		$data=$this->model->order('searchnums','desc')->select();
    		return $this->getAdminTable($data);
    	}
    
    	/**
    	 * 添加
    	 * @return mixed|string
    	 */
    	public function add(){
    		$this->tab_ext=['page_title'=>'添加热搜词',];
    		return $this->addContent();
    	}
    
    	/**
    	 * 修改
    	 * @param null $id
    	 * @return mixed|string
    	 */
    	public function edit($id=null){
    		$this->tab_ext=['page_title'=>'修改热搜词',];
    		if(empty($id)) $this->error('缺少参数');
    		$info=$this->getInfoData($id);
    		return $this->editContent($info,auto_url('index'));
    	}
    
    	/**
    	 * 删除
    	 * @param $ids
    	 */
    	public function delete($ids){
    		if(empty($ids)){
    			$this->error('ID有误');
    		}
    		$ids=is_array($ids)?$ids:[$ids];
    		if($this->model->destroy($ids)){
    			$this->success('删除成功','index');
    		}else{
    			$this->error('删除失败');
    		}
    	}
    }

    要是你看过插件教程 这里肯定不陌生 几乎一模一样

    都是导入了app\common\traits\AddEditList 我们的个性化模块主要就是靠这个实现的 因为他有20多种字段供我们使用并且不需要去创建模型功能

    下期我们着重分析下这个文件 因为现在我也不大会 其实还是等着官方教材比较好

    model 目录

    Content.php 没啥介绍的就是定义了表前缀 这个可以不要 只要把 上面文件中的 $this->model 改为db类即可 不过后台还是建议用模型功能 纯属个人习惯

    install目录

    info.php 模块信息定义了各种信息 自己打开就可以看到有注释

    Install.php 安装文件没有安装流程的可以去掉

    install.sql 安装的SQL 定义了我们的设置参数和表信息

    Uninstall.php 卸载

    hook 目录 模块钩子目录 也可以放到application\common\hook  我个人觉得模块中的钩子调用放到自己的模块下好维护

    关于钩子的下期继续分析 因为钩子太强大了

    为方便大家费劲找 列出前几期和官方文档连接

    钩子简单制作:https://x1.php168.com/bbs/show-503.html

    插件简单制作:https://x1.php168.com/bbs/show-502.html

    模块介绍初级:https://x1.php168.com/bbs/show-501.html

    V系列转X系列:https://x1.php168.com/bbs/show-379.html

    织梦转X系列:https://x1.php168.com/bbs/show-641.html

    官方文档:https://www.kancloud.cn/php168/x1_of_qibo/627066


    13
    赏礼
    赏钱
    收藏
    点击回复
        全部留言
    • 7
    • admin 超级管理员 消费:3.03元 2018-06-01 20:50 20:506楼

      大家可以收藏一下

    0 赏钱 赏礼回复
    • 阿里巴巴 白金粉丝 消费:3000.22元 2018-06-01 20:43 20:436楼

      实用

    0 赏钱 赏礼回复
    • 祯禛 普通粉丝 消费:1286.63元 2018-06-01 17:55 17:556楼

    0 赏钱 赏礼回复
    • kk1212 白金粉丝 消费:1211.05元 2018-05-25 23:28 23:286楼

      不错,学习了

    0 赏钱 赏礼回复
    • 石头 风格开发者 消费:21923.34元 2018-05-25 23:14 23:146楼

      楼主辛苦了


    0 赏钱 赏礼回复
    • verycong 风格开发者 消费:118.73元 2018-05-25 22:08 22:086楼

      比官方的写的明白

    0 赏钱 赏礼回复
    更多回复
    恢复多功能编辑器
  • 3 1
  • 推荐内容
    扫一扫访问手机版
    请选择要切换的马甲:

     
    网页即时交流
    QQ咨询
    咨询热线
    020-28998648