高性能的PHP框架:Yii PHP Framework
- 格式:pdf
- 大小:259.81 KB
- 文档页数:3
深⼊讲解PHP的Yii框架中的属性(Property)在 PHP 中,类的成员变量也被称为属性(properties)。
它们是类定义的⼀部分,⽤来表现⼀个实例的状态(也就是区分类的不同实例)。
在具体实践中,常常会想⽤⼀个稍微特殊些的⽅法实现属性的读写。
例如,如果有需求每次都要对 label 属性执⾏ trim 操作,就可以⽤以下代码实现:$object->label = trim($label);上述代码的缺点是只要修改 label 属性就必须再次调⽤ trim() 函数。
若将来需要⽤其它⽅式处理 label 属性,⽐如⾸字母⼤写,就不得不修改所有给 label 属性赋值的代码。
这种代码的重复会导致 bug,这种实践显然需要尽可能避免。
为解决该问题,Yii 引⼊了⼀个名为 yii\base\Object 的基类,它⽀持基于类内的 getter 和 setter(读取器和设定器)⽅法来定义属性。
如果某类需要⽀持这个特性,只需要继承 yii\base\Object 或其⼦类即可。
补充:⼏乎每个 Yii 框架的核⼼类都继承⾃ yii\base\Object 或其⼦类。
这意味着只要在核⼼类中见到 getter 或 setter ⽅法,就可以像调⽤属性⼀样调⽤它。
getter ⽅法是名称以 get 开头的⽅法,⽽ setter ⽅法名以 set 开头。
⽅法名中 get 或 set 后⾯的部分就定义了该属性的名字。
如下⾯代码所⽰,getter ⽅法 getLabel() 和 setter ⽅法 setLabel() 操作的是 label 属性,:namespace app\components;use yii\base\Object;class Foo extend Object{private $_label;public function getLabel(){return $this->_label;}public function setLabel($value){$this->_label = trim($value);}}(详细解释:getter 和 setter ⽅法创建了⼀个名为 label 的属性,在这个例⼦⾥,它指向⼀个私有的内部属性 _label。
开源PHP开发框架Yii全方位教程(2)控制器CController控制器是CController或者其子类的实例。
控制器在用户请求应用时创建。
控制器执行所请求的action,action通常加载必要的模型并渲染恰当的视图。
最简单的action仅仅是一个控制器类方法,此方法的名字以action开始。
控制器有默认的action。
用户请求不能指定哪一个action执行时,将执行默认的action。
缺省情况下,默认的action名为index。
可以通过设置CController::defaultAction改变默认的action。
下边是最小的控制器类。
因此控制器未定义任何action,请求时会抛出异常。
class SiteController extends CController{}复制代码路由控制器和actions通过ID进行标识的。
控制器ID的格式:path/to/xyz对应的类文件protected/controllers/path/to/XyzController.php,相应的xyz应该用实际的控制器名替换(例如post对应protected/controllers/PostController.php).Action ID与是没有action前缀的action方法名字。
例如,控制器类包含一个actionEdit方法,对应的action ID就是edit。
注意:在1.0.3版本之前,控制器ID的格式是path.to.xyz而不是path/to/xyz。
用户请求一个特定的controller和action用术语即为路由.路由一个controller ID和一个action ID连结而成,二者中间以斜线分隔.例如,route post/edit引用的是PostController和它的edit action.默认情况下,URL http://hostname/index.php?r=post/edit 将请求此controller和action.注意:默认地情况下,路由是大小写敏感的.从版本 1.0.1开始,可以让其大小写不敏感,通过在应用配置中设置CUrlManager::caseSensitive为false.当在大小写不敏感模式下,确保你遵照约定:包含controller类文件的目录是小写的,controller map和action map都使用小写的keys.自版本 1.0.3,一个应用可以包含模块(module).一个module中的controller的route格式是moduleID/controllerID/actionID.更多细节,查阅section about modules.控制器实例化CWebApplication在处理一个新请求时,实例化一个控制器。
PHP的Yii框架中View视图的使⽤进阶视图名渲染视图时,可指定⼀个视图名或视图⽂件路径/别名,⼤多数情况下使⽤前者因为前者简洁灵活,我们称⽤名字的视图为视图名.视图名可以依据以下规则到对应的视图⽂件路径:视图名可省略⽂件扩展名,这种情况下使⽤ .php 作为扩展,视图名 about 对应到 about.php ⽂件名;视图名以双斜杠 // 开头,对应的视图⽂件路径为 @app/views/ViewName,也就是说视图⽂件在yii\base\Application::viewPath 路径下找,例如 //site/about 对应到 @app/views/site/about.php。
视图名以单斜杠/开始,视图⽂件路径以当前使⽤模块的yii\base\Module::viewPath开始,如果不存在模块,使⽤@app/views/ViewName开始,例如,如果当前模块为user, /user/create 对应成@app/modules/user/views/user/create.php,如果不在模块中,/user/create对应@app/views/user/create.php。
如果 yii\base\View::context 渲染视图并且上下⽂实现了 yii\base\ViewContextInterface, 视图⽂件路径由上下⽂的yii\base\ViewContextInterface::getViewPath() 开始,这种主要⽤在控制器和⼩部件中渲染视图,例如如果上下⽂为控制器SiteController,site/about 对应到 @app/views/site/about.php。
如果视图渲染另⼀个视图,包含另⼀个视图⽂件的⽬录以当前视图的⽂件路径开始,例如被视图@app/views/post/index.php 渲染的 item 对应到 @app/views/post/item。
第1篇第一部分:PHP基础知识1. 什么是PHP?简述PHP的历史和特点。
2. PHP与HTML的区别是什么?3. 解释PHP的SAPI(Server API)。
4. PHP的运行环境有哪些?5. 如何设置PHP的运行环境?6. 什么是PHP的版本控制?如何查看PHP版本?7. 解释PHP的变量类型,包括标量类型和复合类型。
8. 什么是变量的作用域?有哪几种作用域?9. 如何声明和初始化一个数组?10. 如何使用关联数组?11. 解释PHP中的魔术方法。
12. 什么是对象?如何创建一个对象?13. 解释面向对象编程(OOP)的三大特性。
14. 什么是封装、继承和多态?15. 解释PHP中的构造函数和析构函数。
16. 什么是类和对象?它们之间的关系是什么?17. 如何实现PHP中的多态?18. 什么是接口?如何使用接口?19. 什么是异常处理?如何使用try-catch块?20. 解释PHP中的魔术引用。
21. 什么是超全局变量?列出常见的超全局变量。
22. 解释PHP中的引用和值传递。
23. 什么是PHP的预定义常量?24. 解释PHP中的类型转换。
25. 什么是PHP的运算符和表达式?26. 如何使用PHP的字符串函数?27. 如何使用PHP的数学函数?28. 解释PHP的日期和时间函数。
29. 什么是PHP的错误处理和日志记录?30. 如何使用PHP的文件和目录函数?第二部分:PHP面向对象编程31. 解释PHP中的抽象类和接口。
32. 什么是继承?如何实现多重继承?33. 如何使用抽象类?34. 解释PHP中的组合和聚合。
35. 什么是PHP中的设计模式?36. 解释单例模式、工厂模式和观察者模式。
37. 如何实现PHP中的静态方法?38. 什么是PHP中的继承和组合?39. 如何使用PHP中的继承和多态?40. 解释PHP中的依赖注入。
41. 什么是设计原则?列举几个重要的设计原则。
42. 解释单一职责原则和开闭原则。
⾼性能PHP框架Symfony2经典⼊门教程Symfony2是⼀个基于PHP语⾔的Web开发框架,有着开发速度快、性能⾼等特点。
本⽂以⼀个程序⽰例的实现过程详细叙述了Symfony2框架的配置与程序开发。
⼀、下载tar zxvf Symfony_Standard_Vendors_2.0.###.tgz -C /var/www上⾯的###是指版本号,我下的时候是BETA5。
当解压之后,Symfony2的⽬录如下:/var/www/ <- Web根⽬录Symfony/ <- Symfony2解压⽬录app/ <- 存放symfony的核⼼⽂件的⽬录cache/ <- 存放缓存⽂件的⽬录config/ <- 存放应⽤程序全局配置的⽬录logs/ <- 存放⽇志的⽬录src/ <- 应⽤程序源代码...vendor/ <- 供应商或第三⽅的模组和插件...web/ <- Web⼊⼝app.php <- ⽣产环境下的前端控制器...如果你需要安装(如果你下载的是without vendor版本)或更新vendor(第三⽅)内容时,可以使⽤:cd /var/www/Symfonyphp bin/vendors install⼆、配置Symfony2的配置很简单,只需要在浏览器中输⼊:http://localhost/Symfony/web/config.php然后按照提⽰来进⾏就可以了。
其中值得注意的就是app/cache和app/logs⽬录的权限问题,由于我是在Ubuntu下安装的,所以可以使⽤(其中firehare是我的⽤户名,⼤家在这⾥可以⽤你的⽤户名代替):#为了保险起见rm -rf app/cache/*rm -rf app/logs/*#设置ACLsudo setfacl -R -m u:www-data:rwx -m u:firehare:rwx app/cache app/logssudo setfacl -dR -m u:www-data:rwx -m u:firehare:rwx app/cache app/logs如果系统不⽀持setfacl命令的话,要检查2个地⽅: setfacl是否已经安装,如果没有的话,可以通过以下命令安装(在Ubuntu 11.10中好象已经缺省安装了,包为叫acl):sudo apt-get install setfacl 如果setfacl已经安装,那么请查看/etc/fstab⽂件,看看是否添加了acl选项:# /var was on /dev/sda7 during installationUUID=c2cc4104-b421-479a-b21a-1108f8895110 /var ext4 defaults,acl 0 2 然后根据页⾯提⽰填写数据库名等信息,再将这些信息拷到/var/www/Symfony/app/config/parameters.ini⽂件中,如下所⽰:; These parameters can be imported into other config files; by enclosing the key with % (like %database_user%); Comments start with ';', as in php.ini[parameters]database_driver="pdo_mysql"database_host="localhost"database_name="symfony"database_user="symfony"database_password="symfony"mailer_transport="smtp"mailer_host="localhost"mailer_user=""mailer_password=""locale="zh_CN"secret="29f96e9e70c2797cb77dd088d3954d3c38d9b33f"如果全部OK的话,在你浏览器中输⼊下列地址时,你将得到⼀个Demo页:http://localhost/Symfony/web/app_dev.php三、程序⽰例:1.创建Bundle: ⾸先创建⼀个Bundle:php app/console gen:bundle "AcmeHelloBundle" src 为了确保Acme名称空间可以被⾃动加载,请在你的app/autoload.php⽂件添加下列语句:$loader->registerNamespaces(array(// ...//添加⾃定义的名称空间'Acme' => __DIR__.'/../src',// ...)); 最后是将该Bundle注册到Symfony2中,请在你的app/AppKernel.php⽂件中添加下列语句:// app/AppKernel.phppublic function registerBundles(){$bundles = array(// ...new AcmeHelloBundleAcmeHelloBundle(),);// ...return $bundles;}2.创建路由 路由可以创建在app/config/routing.yml中,但为了有个好的编程习惯和代码组织,可以将它放在所建Bundle⽬录中的Resources/config/routing.yml中,⽽在app/config/routing.yml中只保留到该路由⽂件的引⽤,如下所⽰:# app/config/routing.ymlhomepage:pattern: /defaults: { _controller: FrameworkBundle:Default:index }hello:resource: "@AcmeHelloBundle/Resources/config/routing.yml"⽽真正的路由则写在src/Acme/HelloBundle/Resources/config/routing.yml路由⽂件中,如下所⽰:# src/Acme/HelloBundle/Resources/config/routing.ymlhello:pattern: /hello/{name}defaults: { _controller: AcmeHelloBundle:Hello:index, name:'pu' }3.创建控制器: 控制器的名字⼀定得是HelloController.php,原因很简单,因为你路由已经把控制器的名字给定下来了,在上⾯路由⽂件中的第4⾏和第7⾏中的控制器都是以AcmeHelloBundle:Hello开头的,其中AcmeHelloBundle表⽰Bundle名,⽽Hello则表⽰控制器名,所以控制器必须是HelloController.php,Controller名缀是命名约定。
开源PHP开发框架Yii全方位教程开源PHP开发框架Yii全方位教程(1)应用(Yii::app) (2)开源PHP开发框架Yii全方位教程(2)控制器CController (5)开源PHP开发框架Yii全方位教程(3)模型CModel (9)开源PHP开发框架Yii全方位教程(4)视图View (10)开源PHP开发框架Yii全方位教程(5)组件CComponent (13)开源PHP开发框架Yii全方位教程(6)模块 (16)开源PHP开发框架Yii全方位教程(7)路径别名和命名空间 (19)开源PHP开发框架Yii全方位教程(8)惯例 (21)开源PHP开发框架Yii全方位教程(9)开发流程 (23)开源PHP开发框架Yii全方位教程(11)Active Record(AR) (29)开源PHP开发框架Yii全方位教程(12)片段缓存 (41)开源PHP开发框架Yii全方位教程(13)页面缓存 (44)开源PHP开发框架Yii全方位教程(14)动态内容 (45)开源PHP开发框架Yii全方位教程(15)使用扩展 (46)开源PHP开发框架Yii全方位教程(16)创建扩展 (52)开源PHP开发框架Yii全方位教程(17)使用第三方库 (57)开源PHP开发框架Yii全方位教程(18)定义fixture (58)开源PHP开发框架Yii全方位教程(19)单元测试 (60)开源PHP开发框架Yii全方位教程(20)功能测试 (62)开源PHP开发框架Yii全方位教程(21)自动生成代码 (64)开源PHP开发框架Yii全方位教程(22)URL管理 (71)开源PHP开发框架Yii全方位教程(1)应用(Yii::app)应用代表了整个请求的运行过程。
其主要任务是解析用户请求,并将其分配给相应的控制器以进行进一步的处理。
它同时也是保存应用级配置的核心。
因此,应用一般被称为“前端控制器”。
在入口脚本中,应用被创建为一个单例。
Table of ContentsAbout1 Chapter 1: Getting started with yii2 Remarks2 Versions2 Examples4 Installation or Setup4 API5 Chapter 2: Setting in your main.php file6 Examples6 How to set main.php file in YII Framework V1.6 How to remove index.php in url.6 Chapter 3: Yii Booster8 Examples8 Installation8 Credits9AboutYou can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: yiiIt is an unofficial and free yii ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official yii.The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to ********************Chapter 1: Getting started with yiiRemarksYii is a high-performance PHP framework best for developing Web 2.0 applications.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.Three steps to build your application rapidly:1.You create the database;Yii generates the base PHP code;2.3.You customize the code to fit your exact needs.VersionsSource: Yii #History - Wikipedia (note: release 2.0.9 is missing from the Wikipedia article on 2016-07-29)ExamplesInstallation or SetupSetup for Yii 1.1Step 1 - downloading YiiDownload the Yii framework bundle from the Yii websiteInside the downloaded bundle there are 3 folders, namely:demosframeworkrequirementsdemos, as the name suggests contains a number of demo Yii applications.framework contains the Yii framework. This is the main folder we will use for the setup requirements contains code to check if a server meets the requirements for running YiiCopy the framework folder to your local server. It's recommended to keep the framework folder in the root directory of your application. In this setup guide we will be using localhost/yii-setup/ as our root project directoryStep 2 - the command lineOpen the command line and enter the framework folder. For this example we would go to c:\wamp\www\yii-setup\framework\We will now use yiic to generate a skeleton application. We do this by entering the command: yiic webapp path\to\root\directoryWhere path/to/root/directory will be the path to your root directory, so in our example the command would be:yiic webapp c:\wamp\www\yii-setup\If you receive an error at this point, your command line is not configured to execute php. You will need to enable php execution from the command line to continue. Otherwise, you will be prompted if you would like to create a new application at the entered path. Press y and hit the return key Your Yii skeleton application will be created under the specified pathAPIClass Reference - API v1.0••Class Reference - API v1.1Read Getting started with yii online: https:///yii/topic/1029/getting-started-with-yiiChapter 2: Setting in your main.php file ExamplesHow to set main.php file in YII Framework V1.In the versions of YII Framework Version 1.You will set your main.php File.File Path : application_name/protected/config/main.php<?phpreturn array(// Set Application Name'name' => "Applicaiton Name",// Set Default Controller'defaultController' => 'site/login',// Set Language'language' => 'in',// Set Language for messages and views'sourceLanguage' => 'en',// Set Time Zone'timeZone' => 'Asia/Calcutta',//Charset to use'charset'=>'utf-8',// preloading 'log' component'preload'=>array('log'),//application-level parameters that can be accessed'params'=> array($documentUrl = $baseUrl, // Document URL$documentPath = $_SERVER['DOCUMENT_ROOT'] . '/', // Document Path),);>List of Supported Time Zones - PHPHow to remove index.php in url.Removed the commented lines for rewrite in httpd.conf file.LoadModule rewrite_module modules/mod_rewrite.soYou can modify .htaccess file your application folder.RewriteEngine on# if a directory or a file exists, use it directlyRewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-d# otherwise forward it to index.phpRewriteRule . index.phpafter you can change your main.php file code.<?phpreturn array(// application components'components'=>array(// uncomment the following to enable URLs in path-format'urlManager'=>array('urlFormat'=>'path','showScriptName'=>false,'rules'=>array('<controller:\w+>/<id:\d+>'=>'<controller>/view','<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>','<controller:\w+>/<action:\w+>'=>'<controller>/<action>',),'urlSuffix'=>'.html','caseSensitive'=>false),),);>Read Setting in your main.php file online: https:///yii/topic/6143/setting-in-your-main-php-fileChapter 3: Yii BoosterExamplesInstallationFirst of all download Yii Booster latest end user bundle from here.Download it, unpack its contents to some directory inside your web application. Its recomended to unpack it to the extensions directory. Rename the folder from yiibooster-<version_number> to just yiibooster for convenience.Then we need to configure it. Add below line before return arraybefore return arrayInside components array add:'bootstrap' => array('class' => 'ponents.Booster',),Then preload the yii booster by adding below snippet in the config preload section'preload' => array(... probably other preloaded components ...'bootstrap'),Read Yii Booster online: https:///yii/topic/6369/yii-boosterCreditshttps:///9。
高性能的PHP框架:Yii PHP Framework
Yii是什么
Yii是一个基于组件、用于开发大型Web应用的高性能PHP框架。
它将Web编程中的可重用性发挥到极致,
能够显著加速开发进程。
Yii(读作“易”)代表简单(easy)、高效(efficient)、可扩展(extensible)。
需求
要运行一个基于Yii开发的Web应用,你需要一个支持PHP5.1.0(或更高版本)的Web服务器。
对于想使用Yii的开发者而言,熟悉面向对象编程(OOP)会使开发更加轻松,因为Yii 就是一个纯OOP框架。
信用
Yii的很多想法来自其他著名Web编程框架和应用程序。
下面是一个简短的清单。
Prado:这是Yii的主要思想来源。
Yii采用了基于组件和事件驱动编程模式,数据库抽象层,模块化的应用架构,国际化和本地化,和许多它的其他特点和功能。
Ruby on Rails:Yii继承它的配置的思想。
还引用它的Active Record的ORM设计模式。
jQuery:这是集成在Yii为基础的JavaScript框架。
Symfony:Yii引用它的过滤设计和插件架构。
Joomla:Yii引用其模块化设计和信息翻译方案。
许可
Yii的发布是遵循BSD License的。
这意味着您能免费使用它开发开源或私有的Web 应用程序。
Yii适合做什么?
Yii是一个通用Web编程框架,能够开发任何类型的Web应用。
它是轻量级的,又装配了很好很强大
的缓存组件,因此尤其适合开发大流量的应用,比如门户、论坛、内容管理系统(CMS)、电子商务系
统,等等。
Yii和其它框架比起来怎样?
和大多数PHP框架一样,Yii是一个MVC框架。
Yii以性能优异、功能丰富、文档清晰而胜出其它框架。
它从一开始就为严谨的Web应用开发而精心设
计,不是某个项目的副产品或第三方代码的组合,而是融合了作者丰富的Web应用开发经验和其它热
门Web编程框架(或应用)优秀思想的结晶。
Yii和其它框架比较性能测试
框架/代码类型性能(每秒查询次数)
原生html11318.9fetches/sec
原生php8220.17fetches/sec
CodeIgniter1.7.0655.156fetches/sec
CodeIgniter1.5.4768.199fetches/sec
Zend Framework1.737.9999fetches/sec
Solar1.0.0alpha2243.4fetches/sec
Cakephp1.1.20.7692288.4fetches/sec
Yii1.0.32505.58fetches/sec
Yii可以到原生php的32%,除了比原生php差一点,比其他框架都强不少。
Yii框架Active Record与DAO的性能测试对比
Active Record与DAO都是使用的PDO抽象层
AR插入1W条数据花费时间15.833298921585times
AR删除1W条数据花费时间0.04245400428772times
AR查询1W条数据花费时间0.60363000869751times
DAO插入1W条数据花费时间6.3882400989532times
DAO删除1W条数据花费时间0.0076189041137695times
DAO查询1W条数据花费时间0.048065900802612times
Yii相关链接
Yii官方网站:
Yii文档入口:
Yii豆瓣讨论群组:/group/yii/
Yii中文文档翻译:/p/yiidoc/
文章参考:
/doc/guide/zh_cn/quickstart.what-is-yii /bbs/viewthread.php?tid=107256。