Custom Url Rewriting Not Working

I have the following url /site/page/view/brands/brand_id/99 that should look like this /brandname

So in otherwords the brand id is 99 and the primary key in the brands database, I need to write a custom url where I would get the brandname in the database and use it in the url.

In my main.php config file I have the following


'urlManager'=>array(

               'urlFormat'=>'path',

			'rules'=>array(

                            array(

                                'class' => 'application.components.SolarUrlRule',

                            ),

			),

                        'showScriptName'=>false,

		),

Then I have a file in components called SolarUrlRule.php with the following code




class SolarUrlRule extends CBaseUrlRule

{

    

 

    public function createUrl($manager,$route,$params,$ampersand)

    {

        if (isset($params['view']) && $params['view']==='brands' && $route=='site/page')

        {

            $brand = Yii::app()->db->createCommand("SELECT name FROM ".Yii::app()->params['prefix'].'brand WHERE brand_id='.$params['brand_id'])->queryRow();;


            return $brand['name'];


        }

        return false;  // this rule does not apply

    }


    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)

    {


        if (preg_match('/^(\w+)(\/)(\w+)(\/)(\w+)(\/)(\w+)(\/)(\w+)(\/)(\d+)$/', $pathInfo, $matches))

        {

            //Setting the neccesary GET varaibles

             $_GET['view']=$matches[3];

            $_GET['brand_id']=$matches[11];

            

            

            return 'site/page';

//            return $pathInfo;

        }

        return false;  // this rule does not apply

    } 

}

My problem is that I manage to rewrite the url but when I click on it the page can’t be shown and I get error 404, I’m also not sure what exactly to add in the parseUrl method.

Any help advise will be highly appreciated.

You need to do the reverse of createUrl, that is locate the brand id by name by executing a SELECT query. You will need to adjust the regexp to one word: ‘/^(\w+)$/’ and use $matches[1] as the matched value.

Thank you for the advice and pointing me in the right direction, it worked perfectly!!!!