PHP开发框架Yii 1.1.8 发布。该版本引入了超过80个新功能、加强和bug修复。你可以编写自定义URL规则类来为应用程序处理任意复杂的URL格式,改进的class autoloader等。 Yii是一个高性能的PHP5的web应用程序开发框架。通过一个简单的命令行工具 yiic 可以快速
PHP开发框架Yii 1.1.8 发布。该版本引入了超过80个新功能、加强和bug修复。你可以编写自定义URL规则类来为应用程序处理任意复杂的URL格式,改进的class autoloader等。
Yii是一个高性能的PHP5的web应用程序开发框架。通过一个简单的命令行工具 yiic 可以快速创建一个web应用程序的代码框架,开发者可以在生成的代码框架基础上添加业务逻辑,以快速完成应用程序的开发。
使用自定义url规则类的URL 规则配置:
<ol> <li><span><span>array( </span></span></li> <li><span> // a standard rule mapping '/login' to 'site/login', and so on </span></li> <li> <span> '</span><span><span>action:</span><span>(login|logout|about)</span><span>></span><span>' =</span><span>></span><span> 'site/</span><span><span>action</span><span>></span><span>', </span></span></span> </li> <li><span> </span></li> <li><span> // a custom rule to handle '/Manufacturer/Model' </span></li> <li><span> array( </span></li> <li> <span> 'class' =</span><span>></span><span> 'application.components.CarUrlRule', </span> </li> <li> <span> 'connectionID' =</span><span>></span><span> 'db', </span> </li> <li><span> ), </span></li> <li><span> </span></li> <li><span> // a standard rule to handle 'post/update' and so on </span></li> <li> <span> '</span><span><span>controller:</span><span>\w+</span><span>></span><span>/</span><span><span>action:</span><span>\w+</span><span>></span><span>' =</span><span>></span><span> '</span><span><span>controller</span><span>></span><span>/</span><span><span>action</span><span>></span><span>', </span></span></span></span></span> </li> <li><span>), </span></li> <li><span> </span></li> </ol>
自定义url规则类拓展自 CBaseUrlRule
,可以像如下方式实现:
<ol> <li><span><span>class CarUrlRule extends CBaseUrlRule </span></span></li> <li><span>{ </span></li> <li> <span> public $</span><span>connectionID</span><span> = </span><span>'db'</span><span>; </span> </li> <li><span> </span></li> <li><span> public function createUrl($manager,$route,$params,$ampersand) </span></li> <li><span> { </span></li> <li> <span> if ($</span><span>route</span><span>==='car/index') </span> </li> <li><span> { </span></li> <li><span> if (isset($params['manufacturer'], $params['model'])) </span></li> <li><span> return $params['manufacturer'] . '/' . $params['model']; </span></li> <li><span> else if (isset($params['manufacturer'])) </span></li> <li><span> return $params['manufacturer']; </span></li> <li><span> } </span></li> <li><span> return false; // this rule does not apply </span></li> <li><span> } </span></li> <li><span> </span></li> <li><span> public function parseUrl($manager,$request,$pathInfo,$rawPathInfo) </span></li> <li><span> { </span></li> <li><span> if (preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches)) </span></li> <li><span> { </span></li> <li><span> // check $matches[1] and $matches[3] to see </span></li> <li><span> // if they match a manufacturer and a model in the database </span></li> <li><span> // If so, set $_GET['manufacturer'] and/or $_GET['model'] </span></li> <li><span> // and return 'car/index' </span></li> <li><span> } </span></li> <li><span> return false; // this rule does not apply </span></li> <li><span> } </span></li> <li><span>} </span></li> <li><span> </span></li> </ol>
下载地址:http://www.yiiframework.com/download/
原文出自:开源中国社区