Hello, sorry for thread hijack for I thought creating similar thread with exact same problem is pointless so here is the problem i’m facing,
I started my experiment starting off the Blog example in demo directory, decided to enhance the URL rewrites, so far I have came up with following lines which does the job fine with some small issues
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName' => false,
'rules'=>array(
'post/<id>-<title>'=>'post/view/',
'tag/<tag:.*?>'=>'post/index',
'/'=>'post/index',
'page/<view>'=>'site/page',
'login'=>'site/login',
'contact'=>'site/contact',
),
),
right now my links look like:
mydomain.com
mydomain.com/page/about
mydomain.com/contact
mydomain.com/login
mydomain.com/post/1-A+Sample+Test
I want to make everything in title lowercase and get rid of + sign,
I tried what was posted above but it doesn’t work for me, I know with regular PHP I can create function do it that way but using the Yii framework everything is still foggy, I’ve never been a good manual reader either, I prefer trial and error which helps me learn and not forget so to be honest I haven’t gone through the documentation yet
Edit, ok now I understand my approach was wrong, I still need to write a function or perhaps extend the existing getUrl or createUrl methods so they create the urls to my desire, just like explained in http://www.yiiframework.com/wiki/53/using-search-engine-and-user-friendly-urls
though I’m still having some hard time trying to figure out why my ActiveRecord.php component that I created following the example in the link above is not perhaps active?
Edit2,
figured what’s happening, for the records posting it here
Following the example on http://www.yiiframework.com/wiki/53/using-search-engine-and-user-friendly-urls
I came up with
\protected\components\ActiveRecords.php
class ActiveRecord extends CActiveRecord
{
public function getUrl()
{
$controller=get_class($this);
$controller[0]=strtolower($controller[0]);
$params=array('id'=>$this->id);
// add the title parameter to the URL
if($this->hasAttribute('title')) {
$this->title = preg_replace("`\[.*\]`U","",$this->title);
$this->title = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$this->title);
$this->title = htmlentities($this->title, ENT_COMPAT, 'utf-8');
$this->title = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $this->title );
$this->title = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $this->title);
$this->title = strtolower(trim($this->title, '-'));
$params['title']=$this->title;
}
return Yii::app()->urlManager->createUrl($controller.'/view', $params);
}
}
and
\protected\models\Post.php
changed
class Post extends CActiveRecord
to
class Post extends ActiveRecord
commented out following bit
/*
public function getUrl()
{
return Yii::app()->createUrl('post/view', array(
'id'=>$this->id,
'title'=>seo($this->title),
));
}
*/