Question

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.

Était-ce utile?

La solution

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top