Breadcrumbs.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\page;
  10. use Yii;
  11. use fec\helpers\CUrl;
  12. use fecshop\services\Service;
  13. /**
  14. * Page Breadcrumbs services. 面包屑导航
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Breadcrumbs extends Service
  19. {
  20. public $homeName = 'Home';
  21. public $ifAddHomeUrl = true;
  22. public $active = true;
  23. protected $_items;
  24. public function init()
  25. {
  26. parent::init();
  27. if ($this->active) {
  28. if ($this->homeName) {
  29. $items['name'] = $this->homeName;
  30. if ($this->ifAddHomeUrl) {
  31. $items['url'] = Yii::$service->url->homeUrl();
  32. }
  33. $this->addItems($items);
  34. }
  35. }
  36. }
  37. /**
  38. * property $items|Array. add $items to $this->_items.
  39. * $items format example. 将各个部分的链接加入到面包屑导航中
  40. * $items = ['name'=>'fashion handbag','url'=>'http://www.xxx.com'];.
  41. */
  42. protected function actionAddItems($items)
  43. {
  44. if ($this->active) {
  45. $this->_items[] = $items;
  46. }
  47. }
  48. /**
  49. * 通过上面的方法addItems($items),把item加入进来后
  50. * 然后,通过该函数取出来。
  51. */
  52. protected function actionGetItems()
  53. {
  54. if ($this->active) {
  55. if (is_array($this->_items) && !empty($this->_items)) {
  56. return $this->_items;
  57. } else {
  58. return [];
  59. }
  60. }
  61. }
  62. // generate Breadcrumbs html ,before generate , you should use addItems function to add breadcrumbs items.
  63. /*
  64. protected function actionGenerateHtml(){
  65. $arr = [];
  66. if($this->_items){
  67. foreach($this->_items as $item){
  68. $name = isset($item['name']) ? $item['name'] : '';
  69. $url = isset($item['url']) ? $item['url'] : '';
  70. if($name){
  71. if($url){
  72. $arr[] = '<a href="'.$url.'">'.$name.'</a>';
  73. }else{
  74. $arr[] = '<span>'.$name.'</span>';
  75. }
  76. }
  77. }
  78. }
  79. return $arr;
  80. //if(!empty($arr))
  81. // return implode($this->intervalSymbol,$arr);
  82. }
  83. */
  84. }