Yii Framework Forum: Custom Url Rewriting Not Working - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom Url Rewriting Not Working Rate Topic: -----

#1 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 23 August 2013 - 11:03 AM

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.
0

#2 User is offline   nineinchnick 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 614
  • Joined: 12-September 11
  • Location:Bialystok, Poland

Posted 23 August 2013 - 02:03 PM

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.
Don't be a dick.
0

#3 User is offline   YIINewBy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 41
  • Joined: 11-September 09
  • Location:Pretoria, South Africa

Posted 24 August 2013 - 03:57 AM

View Postnineinchnick, on 23 August 2013 - 02:03 PM, said:

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!!!!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users