Rewrite.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\url;
  10. use fecshop\services\Service;
  11. use fecshop\services\url\rewrite\RewriteMongodb;
  12. use fecshop\services\url\rewrite\RewriteMysqldb;
  13. use Yii;
  14. /**
  15. * Url Rewrite services.
  16. * @author Terry Zhao <2358269014@qq.com>
  17. * @since 1.0
  18. */
  19. class Rewrite extends Service
  20. {
  21. /**
  22. * $storagePrex , $storage , $storagePath 为找到当前的storage而设置的配置参数
  23. * 可以在配置中更改,更改后,就会通过容器注入的方式修改相应的配置值
  24. */
  25. public $storage; // = 'RewriteMongodb'; // RewriteMongodb | RewriteMysqldb 当前的storage,如果在config中配置,那么在初始化的时候会被注入修改
  26. /**
  27. * 设置storage的path路径,
  28. * 如果不设置,则系统使用默认路径
  29. * 如果设置了路径,则使用自定义的路径
  30. */
  31. public $storagePath = '';
  32. protected $_urlRewrite;
  33. public function init()
  34. {
  35. parent::init();
  36. // 从数据库配置中得到值, 设置成当前service存储,是Mysqldb 还是 Mongodb
  37. $config = Yii::$app->store->get('service_db', 'url_rewrite');
  38. $this->storage = 'RewriteMysqldb';
  39. if ($config == Yii::$app->store->serviceMongodbName) {
  40. $this->storage = 'RewriteMongodb';
  41. }
  42. $currentService = $this->getStorageService($this);
  43. $this->_urlRewrite = new $currentService();
  44. }
  45. /**
  46. * @param $urlKey | string
  47. * 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
  48. */
  49. protected function actionGetOriginUrl($urlKey)
  50. {
  51. return $this->_urlRewrite->getOriginUrl($urlKey);
  52. }
  53. /**
  54. * get artile's primary key.
  55. */
  56. protected function actionGetPrimaryKey()
  57. {
  58. return $this->_urlRewrite->getPrimaryKey();
  59. }
  60. /**
  61. * get artile model by primary key.
  62. */
  63. protected function actionGetByPrimaryKey($primaryKey)
  64. {
  65. return $this->_urlRewrite->getByPrimaryKey($primaryKey);
  66. }
  67. //public function getById($id){
  68. // return $this->_article->getById($id);
  69. //}
  70. /**
  71. * @param $filter|array
  72. * get artile collection by $filter
  73. * example filter:
  74. * [
  75. * 'numPerPage' => 20,
  76. * 'pageNum' => 1,
  77. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  78. * 'where' => [
  79. * ['>','price',1],
  80. * ['<=','price',10]
  81. * ['sku' => 'uk10001'],
  82. * ],
  83. * 'asArray' => true,
  84. * ]
  85. */
  86. protected function actionColl($filter = '')
  87. {
  88. return $this->_urlRewrite->coll($filter);
  89. }
  90. /**
  91. * @param $one|array , save one data .
  92. * @param $originUrlKey|string , article origin url key.
  93. * save $data to cms model,then,add url rewrite info to system service urlrewrite.
  94. */
  95. protected function actionSave($one)
  96. {
  97. return $this->_urlRewrite->save($one);
  98. }
  99. /**
  100. * @param $ids | Array or String or Int
  101. * 删除相应的url rewrite 记录
  102. */
  103. protected function actionRemove($ids)
  104. {
  105. return $this->_urlRewrite->remove($ids);
  106. }
  107. /**
  108. * @param $time | Int
  109. * 根据updated_at 更新时间,删除相应的url rewrite 记录
  110. */
  111. protected function actionRemoveByUpdatedAt($time)
  112. {
  113. return $this->_urlRewrite->removeByUpdatedAt($time);
  114. }
  115. /**
  116. * 返回url rewrite model 对应的query
  117. */
  118. protected function actionFind()
  119. {
  120. return $this->_urlRewrite->find();
  121. }
  122. /**
  123. * 返回url rewrite 查询结果
  124. */
  125. protected function actionFindOne($where)
  126. {
  127. return $this->_urlRewrite->findOne($where);
  128. }
  129. /**
  130. * 返回url rewrite model
  131. */
  132. protected function actionNewModel()
  133. {
  134. return $this->_urlRewrite->newModel();
  135. }
  136. }