手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP Framework

Yii ClinkPager 郁闷

开发的时候,分页用了CLinkPager,然而在某一个页面的时候,page一直在变,但是下面的分类中的当前页永远在1上,不随着页码的变化而变化。
开始的时候以为是分页程序有问题,仔细对应了一下,发现分页的数据是正确的。
排查了半个多小时,突然想起,会不会$_GET['page']被unset了?
找了一下页面,最后在模版页里居然真的发现了unset($_GET['page']),当时我就傻眼了。
顺便再上一个option的onchange切换函数。很烂,只求先解决问题。。。。

JavaScript代码
  1. function urlchange(field,value) {  
  2.     var href= location.href;  
  3.     var regex = new RegExp(field+"=[\-|0-9]{0,}");  
  4.     if(href.indexOf(field)!=-1){  
  5.         location.href = href.replace(regex,field+"="+value);  
  6.     }else{  
  7.         location.href = href + "&"+field+"="+value;  
  8.     }  
  9. }      

用法就是<select onchange="urlchange('page',this.value)"><option value='1'>第一页</option><option value='2'>第二页</option><option value='10'>第十页</option></select>

Tags: yii, clinkpager

Yii Rewrite中遇到的困惑

用Yii开发的时候,总是会有涉及到rewrite,否则,网址里一堆 index.php?r=xxxx那就真的太长了。

关于rewrite,可以参考官方的guide,不过上面只有针对apache的htaccess配置。如果是nginx,你还可以搜索一下官方的wiki,有介绍nginx的配置的。只是官方上面用的方法是try_file,只针对高版本的。如果是低版本还是得用 if (!e $request_filename)这类的【更多设置请搜索google】我这里不详细说了。

今天在用官方推出的这一种rewrite功能【Parameterizing Hostnames 】的时候,出了点问题,该功能是:

XML/HTML代码
  1. Parameterizing Hostnames  
  2.   
  3. Starting from version 1.0.11, it is also possible to include hostname into the rules for parsing and creating URLs. One may extract part of the hostname to be a GET parameter. For example, the URL http://admin.example.com/en/profile may be parsed into GET parameters user=admin and lang=en. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.  
  4.   
  5. In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:  
  6.   
  7. array(  
  8.     'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile',  
  9. )  
  10.   
  11. The above example says that the first segment in the hostname should be treated as user parameter while the first segment in the path info should be lang parameter. The rule corresponds to the user/profile route.  
  12.   
  13. Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.  
  14.   
  15. Also note that the rule with parameterized hostname should NOT contain the sub-folder if the application is under a sub-folder of the Web root. For example, if the application is under http://www.example.com/sandbox/blog, then we should still use the same URL rule as described above without the sub-folder sandbox/blog.  

我在rules里写上了 "http://user.xxx.com/<_c:\w+>/<_a:\w+>"=>"user/<_c>/<_a>/"

然后发现访问 http://user.xxx.com/aaa/bbb/id/1的时候出现了404错误,找问题死活找不到在哪里。再转过头去看了很多其他的配置,发现都不正常。

然后,我尝试访问默认页,即没有参数的:http://user.xxx.com/default/index,咦,居然正常了。。但问题随之而来,其他链接都不正常,经过不停的测试测试再测试。最终发现,把""user/<_c>/<_a>/" 中最后一个 / 去掉就正常了。

那么,'<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>/'  和  '<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>' 的区别是什么呢?

仔细看了一下生成的链接,如果用第二种的时候,如果有多个参数,就转为 m/c/a?a=b&c=d 而如果用第一种就是生成 m/c/a/a/b/c/d 。这时候,系统可能会识别不出我这种用二级域名做rewrite的方式。于是,我去掉了最后一个“/”,一切都正常了。

期间,HM和Horadric.Cube多次对我帮助,告诉我不同的nginx参数的配置。灰常感谢。

Tags: yii, rewrite, rules, route, htaccess

Yii 一行代码,为模块绑定子域名

我在dongbeta的网站上看到了这个东西,突然觉得原来子域名真的很简单,原来我还在想,如何绑定一些子域名的事情呢。

看一下原文吧,然后说明一下,官方的手册中其实已经有类似的介绍了,只是目前这一部分还是英文版的,这可能和这个功能是最新刚加入的关吧。。来源网址为:http://dongbeta.com/view/655

--start--

Yii强大的路由功能配置相当灵活,特别是增加了参数主机名路由功能之后更是可以灵活控制域名。感觉每一次提出一个提议,Yii的开发者qiang如果采纳,就会给出一个我没有想到却更加强大的解决方案。这次就是这样。

下面就是可以将所有子域名都转化为模块的路由规则,来自Yii官方论坛的一个帖子

XML/HTML代码
  1. 'http://<_m:\w+>.xxx.com<_q:.*>/*'=>'<_m><_q>',  
规则写在main.php中的组件配置数组里面:
PHP代码
  1. 'urlManager'=>array(  
  2.     'urlFormat'=>'path',  
  3.     'showScriptName' => false,  
  4.     'rules’=>array(  
  5.         'http://<_m:\w+>.xxx.com<_q:.*>/*'=>'<_m><_q>',  
  6.     )  
  7. ),  

如果仅仅是一个模块(假设是user)需要绑定子域名,可以这样写规则:
XML/HTML代码
  1. 'http://user.xxx.com<_q:.*>/*'=>'user<_q>',  
如果是若干模块需要绑定子域名,可以这样写:
XML/HTML代码
  1. 'http://<_m:(user|admin|photo)>.xxx.com<_q:.*>/*'=>'<_m><_q>',  

在使用的时候我注意到,如果是www开头的网址也会被转向到www模块,那么你可以使用(A|B|C)这样的规则选择要启用的模块,或者使用正则来滤掉www。

另外在路由规则里面,如果某一条匹配,则不会向下进行匹配。这是需要注意的,但是正是这样,路由才更高效,更灵活。

官方的URL管理说明文档:http://www.yiiframework.com/doc/guide/topics.url ,中文的我还没有翻译,等翻译之后,会发布到官方网站。

--EOF--

正如上面作者所说,官方这一段确实还是英文,可以看这里:http://www.yiiframework.com/doc/guide/1.1/zh_cn/topics.url,不过也很容易理解

以下来自官方:

Parameterizing Hostnames

Starting from version 1.0.11, it is also possible to include hostname into the rules for parsing and creating URLs. One may extract part of the hostname to be a GET parameter. For example, the URL http://admin.example.com/en/profile may be parsed into GET parameters user=admin and lang=en. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.

In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:

PHP代码
  1. array(  
  2.     'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile',  
  3. )  

The above example says that the first segment in the hostname should be treated as user parameter while the first segment in the path info should be lang parameter. The rule corresponds to the user/profile route.

Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.

Also note that the rule with parameterized hostname should NOT contain the sub-folder if the application is under a sub-folder of the Web root. For example, if the application is under http://www.example.com/sandbox/blog, then we should still use the same URL rule as described above without the sub-folder sandbox/blog.

 

Tags: yii, 参考

yii-1.1.0-validator-cheatsheet

官方的GUIDE里,对于validator的介绍太少了,只有这么一段 :

7. Validator(验证)

 

Validator需继承CValidator和实现CValidator::validateAttribute方法。

class MyValidator extends CValidator
{
protected function validateAttribute($model,$attribute)
{
$value=$model->$attribute;
if($value has error)
$model->addError($attribute,$errorMessage);
}
}

即使在这里,http://www.yiiframework.com/doc/api/1.1/CValidator,介绍的也不是特别的多,后来,找到了这个cheatsheet,感觉还不错,对应一下api的内容,感觉有点爽

感谢龙井的回复,使得我注意到了wiki,地址:http://www.yiiframework.com/wiki/56/reference-model-rules-validation/,可以关注一下下哦

 

附件: yii-1.1.0-validator-cheatsheet.pdf (60.78 K, 下载次数:3240)

Tags: yii, cheatsheet, validator, framework

Yiiframework(Yii框架)开发笔记:续四


今天我讲两件事,都是关于插件的,1、minify(@hightman的EClientScript) ,2、swfupload
1、EclientScript,这是hightman开发的插件,可以将要发布的css和JS进行合并,方便WEB开发。关于 clientScript,可以查看官方的guide,在性能优化这一节有介绍。但官方的手册上也说了,官方的scriptmap还是需要自己合并。而 eclientScript则实现了这样的功能了。一些使用方法我就不多了,我只说一些存在的问题,直接用代码说,我不说啥了:

XML/HTML代码
  1. <?php  
  2. $css = Yii::app()->clientScript;  
  3. $css->scriptMap = array();  
  4. $css->registerCoreScript('jquery'); //这个是核心的,yii框架里默认带了,你的系统中可以不必存在  
  5. $baseUrl = Yii::app()->request->baseURL ;   
  6. $css->registerScriptFile( $baseUrl . "/xxx/filepath");  
  7. $css->registerCssFile("filepath");  

$baseUrl 这段我遇到的问题最多,开始的时候,我用的是Yii::app()->module->_assetUrl;,事实上我发现这样可以成功,但 生成到assets目录里后,文件就再也不变了(无论你怎么更改原始目录中的文件,assets里的文件都不会更改)。
最后,我是直接把目录扔到了assets的目录里。然后手动设置路径 ,由EclientScript来生成压缩后的css和script文件。(如果需要发布那就必须要用到assetManage)
之所以把目录扔到assets里,是因为我需要单独把assets目录最后用独立域名发布,【关于用独立域名发布,已经建议了hightman更新了,他也答应会更新】
我对eclientscript第二个更新是在输出的URL上加上了filemtime的时间戳。以保证用户可以下载到最新的css和script。

2、swfupload。我是准备把neatpic用Yii重写,而且很多人都想要文件上传功能,但自带的上传功能也只有2M(大多数情况下只有2M)。 在实际使用下来,觉得还成(因为还没有真正测试 phpsession的那个问题,即FF和IE的sessionId的问题)。
swfupload插件,居然把文件分成两个压缩包,没想通。。。如果只下载一个可是不行的哦。所幸,官方把测试用例还是写的较详细(对于flash上的 文字,可以通过widget的参数修改,handlers.js里面是一些completed的一些方式,建议根据自己的需要修改。)

后记:我是邀请了@walkerlee,想让他分享一下心得。他是答应了,所以可以考虑催他了

Tags: framework, yii, 开发笔记