ECJiaWiki:ECJia到家H5开发常见问题
跳到导航
跳到搜索
创建步骤
- 创建主题目录,ecjia/sites/m/content/themes 目录下为主题模板的目录
- 复制ecjia模板文件
- 复制extra目录,function.php文件
- 引入css文件 <link rel="stylesheet" href="{$theme_url}">
- 引入js文件 <script type="text/javascript" src="{$theme_url}" ></script>
基础
路由添加
- route.php文件添加路由规则, 路由从上往下匹配, 添加路由时注意签名有无重复路由
- 路由规则不可添加 index.php , 因为index.php文件已拥有,不会走路由规则
模板使用
- 模板文件以.dwt.php结尾
- {foreach from=$goods_list item=goods}
- {include file="./library/header.lbi.php"}
- {url path="goods/index/show" args="goods_id={$goods.goods_id}"} ==> index.php?m=goods&c=index&a=show&goods_id=466
- {literal}{/literal} 不解析里面的内容 比如js文件中有:会导致解析报错,可使用literal解决
控制器使用
- function.php 方法RC_Hook::add_action('goods/index/show', array('goods_controller', 'show')); 添加控制器 public static function show()注意 static关键字
- 统一使用 ecjia_front静态类 调用公共的模板方法 注:遇到问题换 ecjia
- ecjia_front::$controller->assign('key', $key); 给模板变量赋值
- ecjia_front::$controller->fetch_string($tpl['template_content']); 渲染一段文本
- ecjia_front::$controller->fetch('merchant_comment.dwt'); 渲染一个模板文件
- ecjia_front::$controller->assign_lang(); 导入语言包 注意语言包是否存在
- ecjia_front::$controller->redirect($url); 重定向链接
- ecjia_front::$controller->showmessage('关联用户不存在,请联系管理员', ecjia::MSGTYPE_ALERT | ecjia::MSGSTAT_ERROR); 显示提示信息
- ecjia_front::$controller->is_cached('goods_list.dwt', $cache_id) 判断缓存是否存在
- ecjia_front::$controller->display('goods_list.dwt', $cache_id); 渲染模板
- ecjia_front::$controller->assign_title('商品详情'); 标题
- RC_Upload::upload_url() 子项目upload目录
- RC_Lang::load('wechat'); 载入语言包文件
- RC_Lang::load_plugin('ship_sto_express'); 载入插件
- RC_Loader::load_theme('extras/controller/touch_controller.php'); 引入控制器 参数为路径
- RC_Hook::add_action( 'append_admin_setting_group', array('admin_touch_hooks', 'append_admin_setting_group') ); 第一个参数为function名,第二个数组中:第一个为类名,第二个为function名
模型使用
- RC_model 查询已经逐渐废弃,后续全部使用RC_DB 查询,可以查看相关文档,ECJiaWiki:ECJia到家RC DB-数据库之查询构建器
- 以下为 RC_model 查询说明:
- main_ads_viewmodel extends Component_Model_View 创建模型类
- $this->table_name 主表名
- $this->table_alias_name 主表别名
$this->view = array(
'seller_category' => array( //表名
'type' => Component_Model_View::TYPE_LEFT_JOIN, //连表查询的方式,此为左联
'alias' => 's', //此表的别名
'field' => 'c.cat_id, c.cat_name, c.parent_id, c.is_show, c.show_in_nav, c.sort_order, COUNT(s.cat_id) AS has_children', //查询的字段
'on' => 's.parent_id = c.cat_id' //连表的关系
)
);
- RC_Loader::load_theme ('extras/model/main/main_ads_viewmodel.class.php'); 引入模型文件
- $dbview = new main_ads_viewmodel(); 实例化模型对象
- $dbview
- ->field 查询出的字段
- ->join 联表 array()
- ->where where 条件
- ->order 排序方式 array('id'=>'asc')
- ->limit 限制条数 page, size
- ->select() 查询所有记录
- ->find() 查询单条记录
- ->count('*') 计数
- ->inc('增加的字段', 'where条件', step=1) 按条件将 第一个参数的字段 增加 step
- ->update($data); $data 为数组
- ->insert($data); $data 为数组
- ->get_field('user_id, user_name, email'); 获取多个字段, 以第一个作为键名存入数组
- 使用$dbview 对象进行数据操作,where($where), order(array('rnd' => 'DESC')) , limit(1) , select() $where可以是字符串,可以是数组
- ->last_insert_id 获取最后插入的记录的id
公共函数
- RC_Time::gmtime() = gmtime
- ecjia::config() = C = cfg 相当于大C方法
- RC_Uri::site_url() //项目的主目录
- RC_Time::local_date('Y-m-d') //相当于 date()
- goods_function::get_image_path // 相当于 get_image_path()
- RC_Uri::url // 相当于 url
- RC_String::sub_str //相当于 sub_str
- RC_Time::local_strtotime // 相当于 local_strtotime
- RC_Lang::lang // 相当于 L
- ROUTE_M M参数的值
- RC_Time::local_date // local_date
- RC_ENV::gd_version(); // gd_version()
注意事项
- 导入css文件 ,使用 . - 字符会导致不解析导入样式语句
- foreach语句 item 与 from 变量名不可相同
- 缓存比较严重,有时需要手动删除
- function 对应原model层 里面内容为PC内容,有的需要手动修改
- model文件夹下 为操作数据库的类
- 上传的图片全部放在 content/uploads/下 RC_Upload::upload_url()指向该目录
- 模板用到的图片全部放在 content/themes/主题模板名称/
高级
- 添加function.php 控制器与mvc参数
- 添加控制器
- if (!ecjia_front::$controller->is_cached('user_set_password.dwt', $cache_id)) 判断缓存是否存在
- 查看是否有 model 公共函数 对应extra\controller里面的方法 ,对应控制器同名, 须在function.php文件中注册
- assign 赋值变量
- display 渲染模板
- 添加控制器
RC_Hook::add_action('merchant/index/init', array('merchant_controller', 'init')); 第一个参数为 mca 参数,第二个数组中:第一个为控制其名,第二个为function名
- 错误
$result = new ecjia_error('register', lang) 并使用is_ecjia_error()方法进行判断
$result->get_error_message() 获取错误内容
- 日志使用
RC_Logger::getLogger('pay')->info('支付宝H5交易成功后更新订单'); //error 为日志前缀 支付宝H5交易成功后更新订单 为日志内容 error-2017-11-18.log 为日志文件名,日志文件在 content/storages/logs 目录下
- 添加、自定义类库
RC_Loader::load_theme('extras/classes/user/user_front.class.php'); 引入类库 参数为路径