ecshop 新的重写规则 自定义产品url

2016-07-07 14:55 来源:www.chinab4c.com 作者:ecshop专家



实现产品如下url

www.example.com/book/computer/thinking_in_php

含义:计算机书籍分类下的一本书《php编程思想》

1. 给数据库添加一个新的重写规则

  1. UPDATE `comecshop`.`ecs_shop_config` SET `store_range` = '0,1,2,3' WHERE `ecs_shop_config`.`id` =209 LIMIT 1 ;
复制代码



后台图片1


2. 添加新语言
/language/zh_cn/admin/shop_config.php

  1. $_LANG['cfg_range']['rewrite']['2'] = '复杂重写';
复制代码

的下行添加
  1. $_LANG['cfg_range']['rewrite']['3'] = '特殊重写';
复制代码


后台图片2

回答:
3. 产品表添加新字段goods_url
  1. ALTER TABLE `ecs_goods` ADD `goods_url` VARCHAR(250 ) NOT NULL AFTER `goods_name` ;
复制代码

4. 后台添加产品的表单添加新字段, 效果图如下

修改/admin/template/goods_info.htm
代码
  1. <!-- 通用信息 -->
  2. <table width="90%" id="general-table" align="center">
  3. <tr>
  4. <td class="label">{$lang.lab_goods_name}</td>
  5. <td><input type="text" name="goods_name" value="{$goods.goods_name|escape}" style="float:left;color:{$goods_name_color};" size="30" /><div style="background-color:{$goods_name_color};float:left;margin-left:2px;" id="font_color" onclick="ColorSelecter.Show(this);"><img src="images/color_selecter.gif" style="margin-top:-1px;" /></div><input type="hidden" id="goods_name_color" name="goods_name_color" value="{$goods_name_color}" />
  6. <select name="goods_name_style">
  7. <option value="">{$lang.select_font}</option>
  8. {html_opti** opti**=$lang.font_styles selected=$goods_name_style}
  9. </select>
  10. {$lang.require_field}</td>
  11. </tr>
复制代码

下添加代码
  1. <tr>
  2. <td class="label">自定义URL</td>
  3. <td><input type="text" name="goods_url" value="{$goods.goods_url|escape}" size="35" onblur="checkGoodsUrl(this.value,'{$goods.goods_id}')" /><span id="goods_url_notice"></span></td>
  4. </tr>
复制代码

5. 检查自定义URL是否存在(实现4中出现的checkGoodsUrl函数)

在/admin/templates/goods_info.htm 搜索字符串 checkGoodsSn, 写一个类似的函数了, 就写在她上方,代码如下
  1. /**
  2. * 检查自定义URL是否存在
  3. */
  4. function checkGoodsUrl(goods_url, goods_id)
  5. {
  6. if (goods_url == '')
  7. {
  8. document.getElementById('goods_url_notice').innerHTML = "";
  9. return;
  10. }

  11. var callback = function(res)
  12. {
  13. if (res.error > 0)
  14. {
  15. document.getElementById('goods_url_notice').innerHTML = res.message;
  16. document.getElementById('goods_url_notice').style.color = "red";
  17. }
  18. else
  19. {
  20. document.getElementById('goods_url_notice').innerHTML = "";
  21. }
  22. }
  23. Ajax.call('goods.php?is_ajax=1&act=check_goods_url', "goods_url=" + goods_url + "&goods_id=" + goods_id, callback, "GET", "JSON");
  24. }
复制代码

6. 5中的数据需要通过ajax查询数据库, 继续完成checkGoodsUrl函数的功能
/admin/goods.php
代码段
  1. elseif ($_REQUEST['act'] == 'check_goods_sn')
  2. {
  3. check_authz_json('goods_manage');

  4. $goods_id = intval($_REQUEST['goods_id']);
  5. $goods_sn = json_str_iconv(trim($_REQUEST['goods_sn']));

  6. /* 检查是否重复 */
  7. if (!$exc->is_only('goods_sn', $goods_sn, $goods_id))
  8. {
  9. make_json_error($_LANG['goods_sn_exists']);
  10. }

  11. make_json_result('');
  12. }
复制代码

下添加代码:
  1. elseif ($_REQUEST['act'] == 'check_goods_url')
  2. {
  3. check_authz_json('goods_manage');

  4. $goods_id = intval($_REQUEST['goods_id']);
  5. $goods_url = json_str_iconv(trim($_REQUEST['goods_url']));

  6. /* 检查是否重复 */
  7. if (!$exc->is_only('goods_url', $goods_url, $goods_id))
  8. {
  9. make_json_error('您输入的URL已存在,请换一个');
  10. }

  11. make_json_result('');
  12. }
复制代码

测试下效果:

7. 数据提交入库
/admin/goods.php

以及更新数据

8. 数据提交自定义URL是否重复检查


  1. /* 检查货号是否重复 */
  2. if ($_POST['goods_sn'])
  3. {
  4. $sql = "SELECT COUNT(*) FROM " . $ecs->table('goods') .
  5. " WHERE goods_sn = '$_POST[goods_sn]' AND is_delete = 0 AND goods_id <> '$_POST[goods_id]'";
  6. if ($db->getOne($sql) > 0)
  7. {
  8. sys_msg($_LANG['goods_sn_exists'], 1, array(), false);
  9. }
  10. }

  11. /* 检查自定义URL是否重复 */
  12. if ($_POST['goods_url'])
  13. {
  14. $sql = "SELECT COUNT(*) FROM " . $ecs->table('goods') .
  15. " WHERE goods_url = '$_POST[goods_url]' AND is_delete = 0 AND goods_id <> '$_POST[goods_id]'";
  16. if ($db->getOne($sql) > 0)
  17. {
  18. sys_msg('您输入的URL已存在,请换一个', 1, array(), false);
  19. }
  20. }
复制代码



9.添加分类的URL名
就是www.example.com/book/computer/thinking_in_php中的book和computer

修改ecs_category添加新字段cat_url
  1. ALTER TABLE `ecs_category` ADD `cat_url` VARCHAR( 150 ) NOT NULL AFTER `cat_name` ;
复制代码


模版/admin/template/category_info.htm添加新字段
  1. <tr>
  2. <td class="label">{$lang.cat_name}:</td>
  3. <td>
  4. <input type='text' name='cat_name' maxlength="20" value='{$cat_info.cat_name|escape:html}' size='27' /> <font color="red">*</font>
  5. </td>
  6. </tr>
  7. <tr>
  8. <td class="label">分类URL:</td>
  9. <td>
  10. <input type='text' name='cat_url' maxlength="20" value='{$cat_info.cat_url|escape:html}' size='35' /> <font color="red">*</font>
  11. </td>
  12. </tr>
复制代码


后台编辑分类页面:


js判断下分类URL不能为空:
/admin/template/category_info.htm
  1. /**
  2. * 检查表单输入的数据
  3. */
  4. function validate()
  5. {
  6. validator = new Validator("theForm");
  7. validator.required("cat_name",catname_empty);
  8. validator.required("cat_url",'分类URL不能为空');
  9. if (parseInt(document.forms['theForm'].elements['grade'].value) >10 || parseInt(document.forms['theForm'].elements['grade'].value) < 0)
  10. {
  11. validator.addErrorMsg('{$lang.grade_error}');
  12. }
  13. return validator.passed();
  14. }
复制代码


分类URL的保存
/admin/category.php
insert那段代码

当然更新那段代码也要加上
  1. $cat['cat_url']= !empty($_POST['cat_url'])? trim($_POST['cat_url']): '';
复制代码

update那段代码



10. 修改前台产品URL
现在把后台特殊重写选项选上保存
我们的URL不需要.html了
修改/include/lib_common.php

  1. if ($rewrite && ($rewrite != 3))
  2. {
  3. if ($rewrite == 2 && !empty($append))
  4. {
  5. $uri .= '-' . urlencode(preg_replace('/[\.|\/|\?|&|\+|\\\|\'|"|,]+/', '', $append));
  6. }

  7. $uri .= '.html';
  8. }
  9. if (($rewrite == 2) && (strpos(strtolower(EC_CHARSET), 'utf') !== 0))
  10. {
  11. $uri = urlencode($uri);
  12. }
  13. return $uri;
  14. }
复制代码


接下来通过产品id生成像book/computer/thinking_in_php 这样的字段就完成10了

11.通过产品id生成URL
/includes/lib_common.php
  1. case 'goods':
  2. if (empty($gid))
  3. {
  4. return false;
  5. }
  6. elseif ($rewrite == 3) {
  7. $uri = get_url($gid);
  8. }
  9. else
  10. {
  11. $uri = $rewrite ? 'goods-' . $gid : 'goods.php?id=' . $gid;
  12. }

  13. break;
复制代码


接下来实现get_url函数了,代码就加在/includes/lib_common.php最后面
  1. /**
  2. *
  3. * 特殊重写的URL
  4. * @param int $goods_id 商品id
  5. * @author http://www.heui.org
  6. */
  7. function get_url($goods_id) {

  8. $sql = "SELECT g.goods_url, c.cat_url, c.cat_id, c.parent_id FROM ".$GLOBALS['ecs']->table('goods').
  9. " AS g LEFT JOIN ".$GLOBALS['ecs']->table('category').
  10. " AS c ON c.cat_id = g.cat_id WHERE g.goods_id = ".$goods_id;
  11. $row = $GLOBALS['db']->getRow($sql);
  12. $res = $row['cat_url'].'/'.$row['goods_url'];

  13. while ($row['parent_id']) {
  14. $sql = "SELECT c.cat_url, c.parent_id FROM ".$GLOBALS['ecs']->table('category').
  15. " AS c WHERE c.cat_id = ".$row['parent_id'];
  16. $row = $GLOBALS['db']->getRow($sql);
  17. $res = $row['cat_url'].'/'.$res;
  18. }

  19. return $res;
  20. }
复制代码


把空格替换成减号


include/lib_common.php
  1. if ($rewrite == 2 && !empty($append))
  2. {
  3. $append = str_replace(' ', '-', $append);
  4. $uri .= '-' . urlencode(preg_replace('/[\.|\/|\?|&|\+|\\\|\'|"|,]+/', '', $append));
  5. }
复制代码



12.最后一步了,我们需要保证地址可以访问

goods.php
  1. /*------------------------------------------------------ */
  2. //-- INPUT
  3. /*------------------------------------------------------ */

  4. $goods_id = isset($_REQUEST['id'])? intval($_REQUEST['id']) : 0;

  5. $goods_url = end(explode('/',$_SERVER['REQUEST_URI']));

  6. if ($goods_id == 0 && '' != $goods_url) {

  7. $sql = "SELECT g.goods_id FROM " . $GLOBALS['ecs']->table('goods').
  8. " AS g WHERE g.goods_url = '" . $goods_url . "'";

  9. $goods_id = $GLOBALS['db']->getOne($sql);
  10. $goods_id = is_null($goods_id)?0:$goods_id;
  11. }
复制代码


附上apache重写规则:
  1. RewriteCond $1 \.(gif|jpg|css|js|ico|html)$ [NC,OR]
  2. RewriteCond %{REQUEST_FILENAME} -f [OR]
  3. RewriteCond %{REQUEST_FILENAME} -d
  4. RewriteRule ^(.*)$ - [S=1]
  5. RewriteRule . /goods.php [L]
复制代码

总结: 还有很多细节还要完善了,例如样式和图片的路径不对,这个可以用绝对路径解决,然后分类的路径可能我们也需要改掉,思路都差不多

欢迎大家加入我的QQ群:36504758

想法非常不错,ecshop的收录有待改善,有时间研究一下楼主的代码,看上楼上有一位朋友说自己有更好的方法,但不愿意分享的话还是别在这说了,支持楼主无私的精神

灰常凶悍.........顶顶顶