You are on page 1of 7

Tutorial: Xy dng Breadcrumb Helper trong Codeigniter

Writer: Lu Xun Trng


ang tp tnh SEO nn thy to breadcrumb rt hu dng gp phn cho google d tm v nh ch mc
website ca bn, nn chia s cng cc bn.
y mnh c sn website vit bng CI, cu trc th mc nh sau:
+ bn to file breabcrumb.php ty chnh cu hnh cho breabcrumb.
+ to breabcrumb_helper.php.

1. File cu hnh breadcrumb.php:


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['determine'] = ">"; //du phn cch gia cc link

$config['exclude'] = array();//Nhng tham s trn url m bn b qua.

$config[segment_exclude] = array();// Ch s segment m bn mun b qua.

$config['wrapper'] = "<ul>|</ul>";

$config['wrapper_inline'] = "<li>|</li>";
/*
Cu hnh css, y mnh v d thi cc bn ty bin nha
$config['wraper_style'] = array();
*/

$config['enable_last_link'] = false; // Thit lp tham s cui segment c link khng. Mc nh l


false

?>

2. Code file breabcrumb_helper.php:


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function build_breadcrumb($config = null)


{

$CI =& get_instance();


$CI->load->helper('url');
$CI->config->load('breadcrumb');
$urlString = $CI->uri->uri_string();
$determine = empty($config['determine']) ? $CI->config->item('determine') : $config['determine'];
$exclude = empty($config['exclude']) ? $CI->config->item('exclude') : $config['exclude'];
$segment_exclude = empty($config['segment_exclude']) ? $CI->config->item('segment_exclude') :
$config['segment_exclude'];
$wrapper = empty($config['wrapper']) ? $CI->config->item('wrapper') : $config['wrapper'];
$wrapper_inline = empty($config['wrapper_inline']) ? $CI->config->item('wrapper_inline') :
$config['wrapper_inline'];

$urlArrays = explode("/", $urlString);


array_shift($urlArrays);
$urlArrayTemp1 = Array();
if(!empty($segment_exclude))
{
for ($i = 0; $i < count($urlArrays); $i ++)
{
if(array_search($i, $segment_exclude) === false)
{
$urlArrayTemp1[] = $urlArrays[$i];
}

}
}
else

{
$urlArrayTemp1 = $urlArrays;
}
foreach($exclude as $exc)
{
if(array_search($exc, $urlArrayTemp1) === false)
{
unset($urlArrayTemp1[array_search($exc, $urlArrayTemp1)]);
}
}
// filter number url
foreach ($urlArrayTemp1 as $key => $value)
{
if (preg_match("/^\d+$/", $value))
{
unset($urlArrayTemp1[$key]);
}
}
array_values($urlArrayTemp1);
// Set wrapper
$wrapper = $config['wrapper'] || explode("|", $CI->config->item('wrapper'));
$wrapper_inline = $config['wrapper_inline'] || explode("|", $CI->config>item('wrapper_inline'));
// Begin writing breadcrumb string
$str = $wrapper[0].$wrapper_inline[0].anchor('', 'Home').$wrapper_inline[1];
$segment = '';
$i = 0;

$number_array = count($urlArrayTemp1);
foreach ($urlArrayTemp1 as $value)
{
if ($i > 0)
{
$segment .= $value.'/';

if (strpos($value, "_") OR strpos($value, "-"))


{
$char_to_replace = array ('_', '-');
$value = ucwords(strtolower(str_replace($char_to_replace, " ", $value)));
}

// enable last segment (optional)


if ($i == $number_array-1 && !$CI->config->item('enable_last_segment'))
{
$str .= $determine.$wrapper_inline[0].ucwords($value).$wrapper_inline[1];
break;
}
$str .= $determine.$wrapper_inline[0].anchor($segment, ucwords($value)).
$wrapper_inline[1];
}
$i++;
}
$str .= $wrapper[1];
return $str;

}
?>
3. Load helper breadcrumb:

4. cc file view bn thm dng code sau:


<?= build_breadcrumb();>// nu khng c cu hnh g
Hoc
<?php
$config = Array(
determine => ,
wrappter_css =>shop_breadcrumb
);

echo build_breadcrumb($config);
?.>
Link demo: xuantruong.cz.cc

Chc cc bn hc vui.
Bi sau, mnh s hng dn cc bn lm website a ngn ng vi Zend Framework.

You might also like