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

YUI PHP Loader Utility

YUI是一个低调又强大的框架。你不能说它有什么,但它确实就存在着。。。
jQuery是Yii框架自带的一个工具,比如ZII里集成的全是JQuery,但YUI也是有着自己的工具,比如这个YUI PHP Load Ulility

The YUI PHP Loader Utility is a server-side utility that allows you to load specific YUI components and their dependencies into your page via PHP. YUI PHP Loader can operate as a holistic solution by loading all of your necessary YUI components, or it can be used to add one or more components to a page on which some YUI content already exists.

YUI also contains a client-side loader, which provides similar functionality from JavaScript.

YUI PHP Loader adds value in the following ways:

  1. Reliable, sorted loading of dependencies: YUI comprises more than two-dozen components, many of which work together to provide the best possible compromise between compartmentalization and code reuse. Because of this, YUI components often need to load with specific dependencies in a specific order. YUI PHP Loader understands which components depend on one another, and based on this knowledge it ensures that the right resources are loaded in the right order.
  2. Automatic use of rolled-up files. YUI PHP Loader knows about all of the built-in rollup files that ship with YUI — like the yahoo-dom-event.js file that contains the Yahoo Global Object, the Dom Collection, and the Event Utility, three components that are commonly used together. By automatically using rolled-up files when it makes sense to do so, the YUI PHP Loader helps you reduce HTTP requests and thereby keep your page as efficient as possible.

As you think about how you want to load YUI on the page, you may find it useful to refer to this overview of some of the most common loading strategies and their relative merits:

YUI的CSS其实也不错,现在细想想如果不是当年YAHOO那么强势的推PHP,如今的PHP也不会被这么多公司所能够接受吧。google是在强推python,虽然现在有着go语言,但python也正在被越来越多的公司所接受了,比如它的兼容性,它的运行速度。。。我还是比较喜欢的。。。。。。

Tags: yui, javascript, framework, jquery, yii

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 activerecord 中 index的用法

很多时间,我们需要查询出来的结果按照指定的列进行索引,所以就会有了下面的代码

$lists = xxx::model('xx')->findAll(array('id'=>array(1,2,3,4,5)));//语句应该是不对的,表达我想in查询
然后
取出结果后
foreach($lists as $list){
   $ls[$list->id]=$list;
}
这样有没有简化写法呢?因为我这样之后,发现。。。in查询变成了多条查询。
但是是lazy loading Xxxxx.xxx(relation中的关联)

有的,Yii中有类似的写法,那就是ar中的index(上文我刚说了on)

'index': the name of the column whose values should be used as keys  of the array that stores related objects. 

于是直接

$lists = xxx::model('xx')->findAll(array('index'=>'id'));

就OK了

Tags: yii, framework, ar, index

Yii Relation记录

Yii的relation中遇到的问题,记录一下。。。

目前我有两个表,表不是我设计的。所以我才特别郁闷。

商品表:字段:p_id,member_id

商铺表:字段:shop_id,member_id

其中p_id,shop_id都是主键,member_id是索引

考虑过这个原因,是最初设计的时候,每一个普通用户都可以发布商品,所以商品表没有记录shop_id。

可能也会考虑过,每一个用户会有多个商铺吧,所以shop表的member_id也不是唯一索引。

然后,我用Yii在做表关联,事实上,在我们改动程序的时候,我们已经把普通用户能够发布商品这个功能去掉了,因此,事实上对我们来说member_id,其实肯定是于shop表中的member_id有对应关系。

于是我在ShopProduct的relations这样写

PHP代码
  1. /** 
  2.  * @return array relational rules. 
  3.  */  
  4. public function relations()  
  5. {  
  6.     // NOTE: you may need to adjust the relation name and the related  
  7.     // class name for the relations automatically generated below.  
  8.     return array(  
  9.            'member' => array(self::BELONGS_TO,'ShopMember','member_id','with'=>'extends','condition'=>"member.member_state = '1'"),  
  10.            'shop' => array(self::BELONGS_TO , 'ShopInfo' , 'member_id','on'=>'t.member_id = shop.member_id' ),  
  11.     );  
  12. }  

是的,看上去好象不错,指定了foreignKey为member_id,同时指定了关联条件,即shop.member_id=product.member_id,然而世事总是那样的难以预料。。。。

在Yii的代码中,关于relation的on是这样写的:

XML/HTML代码
  1. 'on': the ON clause. The condition specified here will be appended to the joining condition using the AND operator. This option has been available since version 1.0.2.  

所以,上述的代码在SQL中显示出来就是 select * from product left outer join shop on (product.member_id = shop.shop_id and product.member_id = shop.member_id)//这个代码是伪代码,只是为了说明问题。

问了Yii群和hightman,他们告诉我解决方法都是将foreignKey字段设为空或者False,然后再指定ON。

于是,问题解决。多谢 Fising 和 HightMan

 

 

Tags: yii, framework, relation

Yii使用EClientScript遇到的一点小问题

eclientscript是hightman开发的一个yii插件,用来对于script和css文件进行合并、压缩
当然在自己的项目中,我也为它加了不少的功能和组件,只是因为这是并非公用的东西,拿出来分享意义也不大。
比如,我定义了一个变量path,定义了几个全局的assetUrl,assetPath,然后可以指定目录生成压缩文件,把项目中关于商城、资讯、用户中心等的JS就可以彻底分开了(JS是无所谓,关键是CSS,因为它是认相对目录的)

好了,开始说我遇到的问题吧,目前我有一个JS,二级联动的JS,它是这样写的。

JavaScript代码
  1. (function($){  
  2.    $.fn.xxx = function(){}  
  3. })(jQuery);  

是的,它没有什么问题,而且也挺不错,这样的写法可以防止项目中的$被污染,而使用$只在这一个包里被使用,单独引用的时候,一点问题没有。然而问题就发生在它被合并到一个文件后,上面的代码就无法执行了。。。后来直接改成

XML/HTML代码
  1. $.fn.xxx = function(){}    

好吧,我偷懒了,但,将就点吧,没时间扑在上面。。。招人啊招人啊。。。
本文纯属工作记录,谢谢

Tags: yii, clientscript, jquery