用户
 找回密码
 入住 CI 中国社区
搜索
查看: 5030|回复: 3
收起左侧

[讨论/交流] 自己写的面包屑轨迹实现,求意见

[复制链接]
发表于 2012-5-7 13:21:30 | 显示全部楼层 |阅读模式
本帖最后由 phoenixg 于 2012-5-7 14:24 编辑

水平有限,求意见,高手勿喷

命名成Breadcrumb.php添加进application/libraries/,加载即可


PHP复制代码
 
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
   
/**
 * Bread Crumb Generator
 *
 * @author 爱业星辰
 */

/* 使用方法/测试
默认根据segment
$this->append_crumb();
也可以自定义alias
$this->append_crumb('alias');
*/

class Breadcrumb {
    private $_CI;
    private static $_breadcrumb = array();
    private $_delimiter = ' > ';
    protected $home = 'HOME';
    protected $base_url;
    protected $index = 'index.php';
   
    /**
     * 初始化
     */

    public function __construct()
    {
        //获取CI实例
        $this->_CI = & get_instance();
       
        //获取基础路径
        $this->base_url = $this->_CI->config->item('base_url');
       
        //初始化
        $this->_init_crumb();
       
        //添加log
        log_message('debug', "Breadcrumb Class Initialized");
    }
   
    /**
     * 初始化面包屑数组
     */

    private function _init_crumb()
    {
        $this->_breadcrumb = array(
                                '0' => array($this->home => $this->base_url . $this->index . '/')
        );
    }
   
    /**
     * 添加一条面包屑
     * @param $alias
     */

    public function append_crumb($alias = '')
    {
        $cnt = count($this->_breadcrumb);
       
        if (!in_array($cnt, array('1','2','3')))
        {
            return;
        }
       
        $seg1 = $this->_CI->uri->segment(1,'');
        $seg2 = $this->_CI->uri->segment(2,'');
        $seg3 = $this->_CI->uri->segment(3,'');
       
        $base_url_full = $this->_breadcrumb['0'][$this->home];
       
        if ($cnt == 1)
        {    
            $display = ($alias == '') ? strtoupper($seg1) : strtoupper($alias);
            $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/');
        }
       
        if ($cnt == 2)
        {
            $display = ($alias == '') ? strtoupper($seg2) : strtoupper($alias);
            $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/');
        }
       
        if ($cnt == 3)
        {
            $display = ($alias == '') ? strtoupper($seg3) : strtoupper($alias);
            $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/' . $seg3 . '/');
        }
       
    }
   
    /**
     * 生成面包屑HTML
     */

    public function output()
    {
        $html = '<div>';
        foreach ($this->_breadcrumb as $crumb)
        {
            foreach ($crumb as $display => $addr)
            {
                $html .= '<a href='.$addr.'>' . $display . '</a>' . $this->_delimiter;
            }
        }
       
        $html = rtrim($html, $this->_delimiter).'</div>';
        return $html;
    }
}
 
 
复制代码


发表于 2012-5-7 17:45:20 | 显示全部楼层

$_delimiter也没暴露出来,不想用>怎么办。
 楼主| 发表于 2012-5-7 18:02:02 | 显示全部楼层
本帖最后由 phoenixg 于 2012-5-8 13:07 编辑
yuzhigang5460 发表于 2012-5-7 17:45
$_delimiter也没暴露出来,不想用>怎么办。

谢谢!意见采纳!改进后的版本如下:

PHP复制代码
 
//application/config/breadcrumb.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
$config['breadcrumb_delimiter'] = '&nbsp;>&nbsp;';
$config['breadcrumb_css_id'] = 'breadcrumb';
$config['breadcrumb_html_type'] = 'anchor';//anchor , ul
$config['breadcrumb_home'] = 'HOME';
$config['breadcrumb_index'] = 'index.php';
//application/libraries/Breadcrumb.php
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Bread Crumb Generator
 *
 * @author 爱业星辰
 *
 * Usage:
  $this->breadcrumb->init_crumb(array('DELIMITER' => '&nbsp;>&nbsp;', 'HTML_TYPE' => 'anchor'));//optional (default)
  or
  $this->breadcrumb->init_crumb(array('DELIMITER' => '|', 'HTML_TYPE' => 'anchor')); //optional
  or
  $this->breadcrumb->init_crumb(array('HTML_TYPE' => 'ul')); //optional
 
    $this->breadcrumb->append_crumb();
    or
  $this->breadcrumb->append_crumb('aliasA', 1);
  $this->breadcrumb->append_crumb('aliasB', 2);
  $this->breadcrumb->append_crumb('aliasC', 3);
 
  $this->breadcrumb->generateHTML();
 */

 
class Breadcrumb {
 private static $_breadcrumb = array();
 private $_CI;
 private $_delimiter;
 private $_css_id;
 private $_html_type;
 protected $home;
 protected $index;
 protected $base_url;
 
 /**
  * initialize
  */

 public function __construct()
 {
  //get instance of CI
  $this->_CI = & get_instance();
 
  //load global config file
  $this->_CI->config->load('breadcrumb');
 
  //get config item
  $this->_delimiter = $this->_CI->config->item('breadcrumb_delimiter');
  $this->_css_id = $this->_CI->config->item('breadcrumb_css_id');
  $this->_html_type = $this->_CI->config->item('breadcrumb_html_type');
  $this->home = $this->_CI->config->item('breadcrumb_home');
  $this->index = $this->_CI->config->item('breadcrumb_index');
  $this->base_url = $this->_CI->config->item('base_url');
 
  //initialize crumb array
  $this->_init_crumb_arr();
 
  //add log
  log_message('debug', "Breadcrumb Class Initialized");
 }
 
 /**
  * initialize user defined configuration
  */

 public function init_crumb($config = array())
 {
  $this->_delimiter = isset($config['DELIMITER']) ? $config['DELIMITER'] : $this->_delimiter;
  $this->_html_type = isset($config['HTML_TYPE']) ? $config['HTML_TYPE'] : $this->_html_type;
 }
 
 /**
  * initialize crumb array
  */

 private function _init_crumb_arr()
 {
  $this->_breadcrumb = array(
        '0' => array($this->home => $this->base_url . $this->index . '/')
  );
 }
 
 /**
  * append a crumb
  * @param $alias
  */

 public function append_crumb($alias = '', $segments = null)
 {
  $cnt = count($this->_breadcrumb);
 
  $seg_num = ($segments == null) ? $cnt : $segments;
 
  if (!in_array($seg_num, array('1','2','3')))
  {
   return;
  }
 
  $seg1 = $this->_CI->uri->segment(1,'');
  $seg2 = $this->_CI->uri->segment(2,'');
  $seg3 = $this->_CI->uri->segment(3,'');
 
  $base_url_full = $this->_breadcrumb['0'][$this->home];
 
  if ($seg_num == 1)
  {
   $display = ($alias == '') ? strtoupper($seg1) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/');
  }
 
  if ($seg_num == 2)
  {
   $display = ($alias == '') ? strtoupper($seg2) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/');
  }
 
  if ($seg_num == 3)
  {
   $display = ($alias == '') ? strtoupper($seg3) : strtoupper($alias);
   $this->_breadcrumb[] = array($display => $base_url_full . $seg1 . '/' . $seg2 . '/' . $seg3 . '/');
  }
 
 }
 
 /**
  * generate crumb HTML
  */

 public function generateHTML()
 {
  switch ($this->_html_type) {
   case 'anchor':
    $html = '<div id='.$this->_css_id.'>';
    foreach ($this->_breadcrumb as $crumb)
    {
     foreach ($crumb as $display => $addr)
     {
      $html .= '<a href='.$addr.' target="_blank">' . $display . '</a>' . $this->_delimiter;
     }
    }
   
    $html = rtrim($html, $this->_delimiter).'</div>';
   break;
   
   case 'ul':
    $html = '<ul id='.$this->_css_id.'>';
    foreach ($this->_breadcrumb as $crumb)
    {
     foreach ($crumb as $display => $addr)
     {
      $html .= '<li><a href='.$addr.' target="_blank">' . $display . '</a></li>';
     }
    }
    $html .= '</ul>';
   break;
  }
 
  return $html;
 }
}  
 
 
 
 
复制代码

发表于 2013-8-20 14:44:49 | 显示全部楼层
楼主,这个用的时候怎么用,能给个例子吗?

本版积分规则