Yii new custom url rule class and regex

in the new Yii Custom Url Class





config

array(

    // a standard rule mapping '/' to 'site/index' action

    '' => 'site/index',

 

    // a standard rule mapping '/login' to 'site/login', and so on

    '<action:(login|logout|about)>' => 'site/<action>',

 

    // a custom rule to handle '/Manufacturer/Model'

    array(

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

        'connectionID' => 'db',

    ),

 

    // a standard rule to handle 'post/update' and so on

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

),




class

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

    {

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

        {

            // check $matches[1] and $matches[3] to see

            // if they match a manufacturer and a model in the database

            // If so, set $_GET['manufacturer'] and/or $_GET['model']

            // and return 'car/index'

        }

        return false;  // this rule does not apply

    }



Where do I check for the url pattern of my custom rule?

in my old question it did it easily like this




'product/<category:[\d\w-\/]+>'=>array( // used by EDbUrlManager

                      'product/viewCategory',

                      'type'=>'db',

......



With the new 1.1.8 new custom rule class i tried many

if (preg_match(’%^[\d\w-\/]+?$%’, $pathInfo, $matches))

if (preg_match(’[\d\w-\/]+’, $pathInfo, $matches))

if (preg_match(‘product/<category:[\d\w-\/]+>’, $pathInfo, $matches))

and obviously my regex sucks as idk what I am doing…

I need a help to construct a regex pattern that match "product/cat1/cat2/cat3…cat100…cat200" that will work with the new custom rule.

Yes, you need to improve your regex knowledge. See PCRE on the manual.

This site is very helpful, too.

I have read some, though still not very well and tried

if (preg_match(’[\d\w-\/]+’, $pathInfo, $matches))

which I think is the equivalent of

“product/<category:[\d\w-\/]+>”=>‘controller/action’

Can you help me out?

sorry I meant

if (preg_match(‘product/<category:[\d\w-\/]+>’, $pathInfo, $matches)) … :(

First of all: what do you want to match against?

And second: you need to have delimiters.

Thanks for the delimiter tip! I solved now…

if (preg_match(’%^product/[\d\w-\/]+%’, $pathInfo, $matches)) {

this will match "product/andanything/any-anything/or-any-number-99/123/andanynumberofcategories/andso-on/"

Lastly another quick newbie question:

How to call the db connection inside my customer url? whats the use of

public $connectionID = ‘db’; ??

but I am doing it directly like this :

$connection=Yii::app()->db;

$command=$connection->createCommand($sql);

Notice that your rule may not work for URL generation.

Yes, you are using it right.

I don’t recall now about that variable. Maybe that’s a mistake. I was very new to Yii when the extension was written :D

i dont mean customer url. I am referring to the Yii Custom Url Rule Class, there is public $connectionID = ‘db’; how to use it?

(Did you wrote this new Yii implementation?)

I generate Url like this, look like its wotking though




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

    {

        if ($route==='product/viewCategory')

        {

    			$category = $params['category'];

    			$parts = explode('/', $category);

    			

    			$url = "product/";

    			foreach($parts as $part) {

            $url .= urlencode($part) . '/';

          }

          return $url;

                  

        }

        return false;  // this rule does not apply

    }



No, I’m not a Yii core developer.

That property is for you to set the database component. Usually it will be the default ‘db’.