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

官方wiki:Configuring PhpStorm IDE for Yii

如果到facebook上关注一下yii framework的群组,会发现有人在介绍yiicookbook.org,里面是一本关于1.1的书。
然后去yii framework官方上搜索一下phpstorm,会发现同一个人在说自己用phpstorm,问phpstorm有没有什么支持。结果官网说暂时好象不支持。。。
然后再搜索了一下,发现在yii的官网有一个简单的例子在说如何让项目支持yii framework。看了一下,发现,大多数都用过,只有第一点,说是把yiilite.php过滤掉。这个其实很重要了,否则有些变量就会出现提示两次。
OK,来看官网的wiki吧:http://www.yiiframework.com/wiki/92/configuring-phpstorm-ide-for-yii

Code completion

  1. Exclude yiilite.php from index:
    • File → Settings → IDE Settings → File Types.
    • yiilite.php to Ignore files and folders.
  2. Exclude not used directories, specify resources.
    • File → Settings → Project settings → Directories.
    • Mark framework/cli/views, protected/runtime and assets as excluded.
    • Mark website root as resource root.
  3. Specify path to your PHP.
    • File → Settings → Project settings → PHP → PHP Home.
  4. If your project uses common Yii framework folder you need to include it.
    • File → Settings → Project settings → PHP → PHP Home → Add.
    • Specify a path to framework directory.
  5. If you are writing unit tests you can include PHPUnit to get code completion:
    • File → Settings → Project settings → PHP → PHP Home → Add.
    • Specify a path to PHPUnit.
  • Complete code: Ctrl+Space.
  • Show method arguments: Ctrl+Q.

Testing

You should install PHPUnit to run unit tests.

  1. PHPUnit.
    • Follow official PHPUnit installation guide.
    • In your IDE: Run → Edit configurations.
    • Press "+".
    • Name: anything.
    • Test: depending on what do you want to test select an appropriate option. Specify path.
    • Use XML configuration file: specifying path to phpunit.xml. Often it's path_to_your_webroot/protected/tests/phpunit.xml.
  • To run tests use SHIFT+F10.
OK,现在这样就可以简单的支持yii framework了。只是phpstorm还是有点小问题,因为提示的时候不会显示phpdoc的信息。纠结,啥时候会有doc的提示呢?

Tags: phpstorm, yii, facebook

Yii + Zend_Feed

YII是一个PHP框架。
ZendFramework也是一个PHP框架
在Yii里配置ZF框架,刷刷的就一个RSS阅读器就出来了。
代码很简单,把Zend整合COPY到protected/extensions目录下
在控制器的init方法里加入:
Yii::import("ext.*");
require_once("Zend_Feed_Reader.php");

然后在需要的方法里加入
$feed = Zend_Feed_Reader::import("http://neatstudio.com/rss.php");
这样就创建了一个读的对象了。然后。。。。。
你懂的:

PHP代码
  1. $data = array(  
  2.     'title'        => $feed->getTitle(),  
  3.     'link'         => $feed->getLink(),  
  4.     'dateModified' => $feed->getDateModified(),  
  5.     'description'  => $feed->getDescription(),  
  6.     'language'     => $feed->getLanguage(),  
  7.     'entries'      => array(),  
  8. );  
  9.   
  10. foreach ($feed as $entry) {  
  11.     $edata = array(  
  12.         'title'        => $entry->getTitle(),  
  13.         'description'  => $entry->getDescription(),  
  14.         'dateModified' => $entry->getDateModified(),  
  15.         'authors'       => $entry->getAuthors(),  
  16.         'link'         => $entry->getLink(),  
  17.         'content'      => $entry->getContent()  
  18.     );  
  19.     $data['entries'][] = $edata;  
  20. }  
  21. print_r($data);  

就这样,一个刚刚的RSS阅读器就出来了。
当然,它没有缓存,没有这没有那的,不过,都解析完了,剩下的功能还会远吗?

Tags: yii, zend

扩展yii的验证类

在项目中遇到文件上传的问题,这时候需要用到CFileValidator,但是官方的验证中少了一点点的处理,比如,对于图片,我只想上传尺寸正好是300x500的图片。怎么办?
所以我做了一点小小的扩展,于是我在rules里面加了这么两条:

PHP代码
  1. array('picname''application.validators.Myfile''on'=>'insert','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024'),  
  2.          array('picname''application.validators.Myfile''on'=>'update','types'=>'jpg,png','wrongType'=>'只允许上传jpg或者PNG','maxSize' => 1024*300,'tooLarge' => '图片最大只支持300K','imageSize'=>'768x1024','wrongImageSize'=>'对不起,图片尺寸只支持768*1024','allowEmpty'=>true,),  

数组的第二个字段就是一个全路径,告诉rules,对于picname用的是application.validators.Myfile类。这个类其实很简单,只是简单的扩展了官方的CFileValidator类,大致如下:

PHP代码
  1. class Myfile extends CFileValidator{  
  2.     public $imageSize;  
  3.     public $wrongImageSize;  
  4.   
  5.     /** 
  6.      * @param CModel $object the object being validated 
  7.      * @param string $attribute the attribute being validated 
  8.      * @param CUploadedFile $file uploaded file passed to check against a set of rules 
  9.      * @return void 
  10.      */  
  11.     protected function validateFile($object$attribute$file){  
  12.         parent::validateFile($object,$attribute,$file);  
  13.         if($this->imageSize!=''&&strpos($this->imageSize,"x")!==false){  
  14.             list($width,$height) = @getimagesize($file->getTempName());  
  15.             $imageSize = sprintf("%sx%s",$width,$height);  
  16.             if($imageSize != $this->imageSize){  
  17.                 $message=$this->wrongImageSize!==null?$this->wrongImageSize : Yii::t('yii','The file "{file}" cannot be uploaded. Only files with these size are allowed: {imagesize}.');  
  18.                 $this->addError($object,$attribute,$message,array('{file}'=>$file->getName(), '{extensions}'=>$this->imageSize));  
  19.             }  
  20.         }  
  21.     }  
  22. }  

我这种只是简单的判断是否与指定尺寸相符,如果需要检测小于指定范围的图片,那就需要多一点的判断了,也不会太难啦。主要是一个思路(感谢烂桔提供思路)

Tags: yii

在Yii框架中使用Hprose或PHPRPC

Yii:

XML/HTML代码
  1. Yii is a high-performance PHP framework best for developing Web 2.0 applications.  
  2.   
  3. Yii comes with rich features: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc. It can reduce your development time significantly. 

PHPRPC:

XML/HTML代码
  1. PHPRPC 是一个轻型的、安全的、跨网际的、跨语言的、跨平台的、跨环境的、跨域的、支持复杂对象传输的、支持引用参数传递的、支持内容输出重定向的、支持分级错误处理的、支持会话的、面向服务的高性能远程过程调用协议。  
  2.   
  3. 目前该协议的最新版本为 3.0。该版本目前已有以下几种语言的实现:  
  4.   
  5.     ASP,ActionScript,Delphi/C++Builder/Kylix,Java,JavaScript,NET,PHP,Python,Ruby,Perl,Lazarus(Free Pascal)  
  6.   
  7. 其中 ASP、.NET、Java、Ruby、Python 和 PHP 版本除了提供客户端实现外,还提供了服务器端实现。  

HPROSE:

XML/HTML代码
  1. Hprose (High Performance Remote Object Service Engine) 是一个商业开源的新型轻量级跨语言跨平台的面向对象的高性能远程动态通讯中间件。它支持众多语言,例如 C++, .NET, Java, Delphi, Objective-C, ActionScript, JavaScript, ASP, PHP, Python, Ruby, Perl 等语言,通过 Hprose 可以在这些语言之间实现方便且高效的互通。  
  2.   
  3. Hprose 易学易用,且功能强大,您只需很短时间的学习,就可以用它方便地构建出跨语言跨平台分布式的电信级应用系统。  

如何在Yii中使用Hprose或者PHPRPC呢?我这里简单介绍一下Yii下hprose的应用方法

 

1、下载hprose的PHP版本,嗯,别看了,是混淆过的,但不影响你使用。

2、将下载后的hprose拷贝到yii项目的/protected/extension/hprose目录下

3、新建一个apicontroller:

大小: 43.99 K
尺寸: 299 x 376
浏览: 1756 次
点击打开新窗口浏览全图

我这样的方式是最简单的,将一些actionIndex的方法都公开了,在实际应用中当然不可以这样。不过我这里是测试,当然是无所谓的

访问http://localhost/project/test/ (project是我的项目名称,test是controller, index是action,由于默认action是index,所以我省略了)

浏览器中返回了:

XML/HTML代码
  1. Fa4{s11"actionIndex"s11"afterAction"s5"hello"s3"sum"}z  

由此其实我们也可以基本确认有四个方法可以使用actionIndex,afterAction,hello,sum,这主要是因为我把当前类当成发布对象了。(不主张啊不主张)

 

4、继续下一步,

PHP代码
  1. <?php
  2. include("./protected/extensions/hprose/hproseHttpClient.php");  
  3. $client = new HproseHttpClient("http://localhost/project/test");  
  4. $result = $client->sum(1,2,3,4,5);  
  5. print_r($result);  

 

OK,浏览器输出了15,就这样完成了一个简单的HPROSE应用。

当然我这样的方式太简单了,文档在这里有下载,我就不再画蛇添足的提供了:http://www.hprose.com/documents.php。

因为只是简单的的一些应用,暂时也看不到hprose的性能比phprpc提升了多少。

---------------备注

由于Yii对于大小写敏感,因此在new HproseHttpServer();的时候,Hprose的H不能大写,否则,它会自动寻找HproseHttpServer.php,但由于PHP的hprose的文件中首字母都是小写。因此在new的时候就需要:new hproseHttpServer();

Tags: yii, hprose, phprpc

yii的一些小技巧

笔记。。。
Yii的cache大家用起来可能是比较爽,直接Yii::app()->cache->get,就over了。在这个时候,我们可以配置相应的cache方式,比如数据库,memcache,apc,eacc.....(太长,记不住),等等。但是,怎么样能够让memcache缓存与文本缓存或其他缓存共用呢?

所以,不要纠结于普通的想法。嗯,在main.php的cache数组元素下,建一个同级数组。如:
        'fc' => array(
            'class' => 'CFileCache',
            'directoryLevel'=>1,
        ),
然后在项目中就可以直接Yii::app()->fc->get,或者Set了。是不是很方便 ?

Tags: yii, memcache, filecache, apc