Yii r1.8 Using Custom URL Rule Classes

I am trying to take advantage of the Custom URL Rule Classes in Yii r1.8

I am trying to take something that looks like orgs/view/id/24 and instead display the name of the org as identified by Name in the db (i.e. changing www.mysite.com/orgs/view/id/24 to www.mysite.com/jaysshop dynamically ). Unfortunately I am not getting it to work.

Here is my code:


<?php 

class OrgsUrlRule extends CBaseUrlRule

{

	public $connectionID = 'db';


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

	{

		if ($route==='orgs/view/id')  //even tried 'orgs/view' or 'orgs/index'

		{

			if (isset($params['Name']))

				return $params['Name'];

			else if (isset($params['Name']))

				return $params['Name'];

		}

		return false;  

	}


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

	{

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

		{

		}

		return false;  

	}

}

?>

urlManager:


array(

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

  'connectionID' => 'db',

),

Anyone else tried this yet? Or can pick out what the problem is with mine?

Thanks

What does not work createUrl() or parseUrl()

What are the urlRules before this one…

Here is all that is above…


'urlManager'=>array(

  'urlFormat'=>'path',

  'showScriptName'=>false,

  'rules'=>array(

    ''=>'site/index', 

    array(

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

      'connectionID' => 'db',

    ),

Right now all I am trying to do is get the url created (i.e. go from orgs/view/id/24 to jaysshop)

What is you code you use to create a link?

Am I missing something here? I thought that is the purpose of


class OrgsUrlRule extends CBaseUrlRule

that I posted above along with the urlManager rule. Do I need something else somewhere?

I was thinking on the code to create/display a link…

For example




<?php

   echo CHtml::link('link',array('orgs/view','id'=>24,'Name'=>'dada'));

?>



would create a link to "yourapp/data"

Ok mdomba, I get what you are saying but I thought this was a dynamic way of creating urls. According to the docs and included example of manufacturer/model that is what it appears to me. With all the orgs in my db I don’t want the users to see the full url of www.mysite.com/orgs/view/id/24. Instead www.mysite.com/jaysshop

Or I don’t understand you… or you don’t understand me…

One thing is the url rule to create a link… another thing is to create a link…

In your applications… there are many places where the links are created… and to be created url rules are used…

When you want to create a rule for "orgs/view" like in my example above… your custom rule will be used…

In your custom rules the $params array are the parameters sent to the link…

so again for my example I sent ‘id’ and ‘Name’…

then in the custom rule you can use $param[‘id’] to get the value of id and $param[‘Name’] to get the value of Name

I hope you understand it now …

Your example of turning "www.mysite.com/orgs/view/id/24" to "www.mysite.com/jaysshop" is not good…

what would be the link for another record like 20 - "www.mysite.com/orgs/view/id/20"?

Thanks for the further explanation mdomba. That does help my understanding of the url creation process but not the inability of having the urlManager rules handling what I want to accomplish.

Maybe I am just not understanding the capabilities of Custom URL Rule Classes. With this statement in the docs though


"For example, in a car dealer website, we may want to support the URL format like

`/Manufacturer/Model`, where `Manufacturer` and `Model` must both match some data in

a database table."

I read that as being what I want to accomplish.

Others have figured out how to accomplish what I want with a variable. Maybe I need to go that route then and assign a variable to my orgs.Name? Although I would rather stick to this added functionality in r1.8 if it does indeed support what I am after.

Anybody else have any ideas with this?