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:
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 $para开发者_开发百科ms['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',
),
I'm giving you this example in the assumption that you want to take the name of a shop from the URL trigger the your controller/action on it
For example:
public function parseUrl($manager,$request,$pathInfo,$rawPathInfo)
{
$aParts = explode('/', $pathInfo);
if (count($aParts) == 1) // It's only 1 piece, so a possible "shop name"
{
if (isAValidNameForAShop($aParts[0]))
{
$_REQUEST['id'] = $aParts[0]; // Store it to retrieve it in the controller
return 'orgs/view';
}
}
return FALSE; // Seems like something else, we don't apply
}
Hope that helps you along a bit.
精彩评论