0

goodmorning,

i have yet another problem with my website. i've developed a site that is similar to yellowpages.com (well not so similar but is just to have an idea)

now they asked me to make some crazy stuff with SEO and url-rewriting.

i'll start with my main.php

'urlManager'=>array(
        'urlFormat'=>'path',
                    'showScriptName'=>false,
        'rules'=>array( 
                            'surf/category/<id:[0-9]+>/page/<page:[0-9]+>'=>'naviga/categoria/',
                            'surf/subcategory/<id:[0-9]+>/page/<page:[0-9]+>'=>'naviga/sottocategoria/',
                            'surf/page/<page:[0-9]+>'=>'surf/',
                            'contact' =>'site/contact/',
                            'write-mail/<id:[0-9]+>' =>'site/contact/',
                            'privacy' =>'site/page/view/privacy',
                            'register'=>'site/page/view/register',

                            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

                        ),
    ),

now the request ..... T_T they want that the url will be something like that:

http://domain.com/category-name/ instead of http://domain.com/surf/category/3 (ex.)

http://domain.com/category-name/sub-category-name/ instead of http://domain.com/surf/subcategory/3

http://domain.com/category-name/sub-category-name/society-name/ instead of http://domain.com/detail/2

and obviously the rest of the link must be working with the last 3 controller rules.... somebody can help me??? i'm in a really tight spot....they'll kick my ass if i can't find a solution for tuesday.....

T_T

thanks in advance for the help.

tereško
  • 58,060
  • 25
  • 98
  • 150
Aliceiw
  • 420
  • 5
  • 19

1 Answers1

8

What your after is out of the scope of yii's basic regex based url rules. What you will need is your own custom url rule classes.

In config:

'rules'=>array( 
    array(
        'class'=>'application.components.CategoryUrlRule'
    ),
)

In protected/components/CategoryUrlRule.php:

class CategoryUrlRule extends CBaseUrlRule { 
    public function createUrl($manager,$route,$params,$ampersand) {
        if ($route==='naviga/categoria') {
            return $params['categoryname'];
        }elseif ($route==='naviga/sottocategoria') {
            return $params['categoryname'].'/'.$params['subcategoryname'];
        }else{
            return false;  // this rule does not apply
        }
    }

    public function parseUrl($manager,$request,$pathInfo,$rawPathInfo) {
        if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) {
            $category=$matches[1];
            if(!empty($matches[2])
                $subcategory=$matches[2];

            // Check in db
            Yii:app()->db-> //...

            if(){ // There is a match from db check above
                if(isset($subcategory)){
                    $_GET['subcategory']=$subcategory;
                    return 'naviga/sottocategoria';
                }else{
                    $_GET['category']=$category;
                    return 'naviga/categoria';
                }
            }
        }
    }
}

Using Custom URL Rule Classes

Olly Hicks
  • 1,104
  • 1
  • 9
  • 13
  • this just create a new rules check and don't overwrite the existing rules right? – Aliceiw Jan 09 '12 at 11:16
  • sort of, in your array of url rules each one will be checked to see if the request url matches. So if your first url matched everything the others would never be seen, unless the first rule returned false – Olly Hicks Jan 23 '12 at 09:51
  • How can I make the custom rule apply to only request type ? – Varun Jain Dec 21 '12 at 15:05