This helper will let you create search engine friendly urls without having you to change the controller logic.
Lets assume we have an 'articles' controller (/app/controllers/articles_controller.php) in our website. And the database table looks like this:
id, category_id, title, content, updated, created
The current url structure:
yoursite.com/articles/category/[category-id]/
yoursite.com/articles/view/[article-id]/
So, it means we dont have the 'title' of our articles included in the url. Only ID is present. This helper will let you add the title as well in the url.
Helper class
<?
class SefHelper extends Helper {
var $helpers = array('Html');
function clean($string) {
return r(" ", "-", strtolower($string));
}
function article($array) {
$link = "/articles/view/";
$link .= $array['Article']['id'] . "/";
$link .= $this->clean($array['Article']['content_title']) . "/";
return $link;
}
function category($array) {
$link = "/articles/category/";
$link .= $array['Category']['id'] . "/";
$link .= $this->clean($array['Category']['title']) . "/";
return $link;
}
?>
class SefHelper extends Helper {
var $helpers = array('Html');
function clean($string) {
return r(" ", "-", strtolower($string));
}
function article($array) {
$link = "/articles/view/";
$link .= $array['Article']['id'] . "/";
$link .= $this->clean($array['Article']['content_title']) . "/";
return $link;
}
function category($array) {
$link = "/articles/category/";
$link .= $array['Category']['id'] . "/";
$link .= $this->clean($array['Category']['title']) . "/";
return $link;
}
?>
View
<? echo $html->link("Article Title here", $sef->article($articles_result)); ?>
The return of this helper function will be: /articles/view/[id]/[title-of-the-article]
1 comments:
ve used this and it is working well, thanks. But the pagination does then not include the new SEF URL. Do you know how to also add the SEF URL in the pagination links?
Thanks
Post a Comment