Yii学习总结之安装配置
《:Yii学习总结之安装配置》要点: PHP应用之前写过Yii的文章,正好假期没啥事,就结合以前的文章,Yii的官方文档,再加上最近的关于Yii的收获总结一下,写个系列~~ Yii是一个基于组件的高性能PHP框架,用于开发大型Web应用.Yii采用严格的OOP编写,并有着完善的库引用以及全面的教程.从 MVC,DAO/ActiveRecord,widgets,caching,等级式RBAC,Web服务,到主题化,I18N和L10N,Yii提供了今日Web 2.0应用开发所必要的几乎一切功能.事实上,Yii是最有效率的PHP框架之一.Yii是一个高性能的PHP5的web应用程序开发框架.通过一个简单的命令行工具 yiic 可以快速创建一个web应用程序的代码框架,开发者可以在生成的代码框架基础上添加业务逻辑,以快速完成应用程序的开发. 安装Yii 在安装Yii之前,你必须配置好你的开发环境,如一台支持PHP5.1.0以上版本的Web服务器.Yii已经在Windows和Linux操作系统上的 Apache Web服务器测试通过.它可能也会运行在其他平台上的支持PHP5的Web服务器,互联网上公布了很多免费资源,你可能会获得一个配置好PHP5的Web 服务器环境.在这里我们会抛开Web服务器和PHP5的安装. 创建一个新的应用法式
代码如下:
? % cd Webroot/testdrive/framework ? % yiic webapp ../../testdrive ? Create a Web application under '/WebRoot/testdrive'? [Yes|No] ? Yes ???????? mkdir /WebRoot/testdrive ???????? mkdir /WebRoot/testdrive/assets ???????? mkdir /WebRoot/testdrive/css ???????? generate css/bg.gif ???????? generate css/form.css ???????? generate css/main.css 你的应用已经成功创建到了/WebRoot/demo下.这个webapp命令的作用是创建一个全新的Yii应用.它只必要指定一个参数,无论是绝对还是相对路径都会创建应用程序.它所生成的目录及文件只是应用程序的一个骨架.
代码如下:
testdrive/ ?? index.php???????????????? Web 应用入口脚本文件 ?? index-test.php??????????? 功能测试使用的入口脚本文件 ?? assets/?????????????????? 包含公开的资源文件 ?? css/????????????????????? 包含 CSS 文件 ?? images/?????????????????? 包含图片文件 ?? themes/?????????????????? 包含应用主题 ?? protected/??????????????? 包含受保护的应用文件 ????? yiic?????????????????? yiic 命令行脚本 ????? yiic.bat?????????????? Windows 下的 yiic 命令行脚本 ????? yiic.php?????????????? yiic 命令行 PHP 脚本 ????? commands/????????????? 包含自定义的 'yiic' 命令 ???????? shell/????????????? 包含自定义的 'yiic shell' 命令 ????? components/??????????? 包含可重用的用户组件 ???????? Controller.php????? 所有控制器类的基础类 ???????? Identity.php??????? 用来认证的 'Identity' 类 ????? config/??????????????? 包含配置文件 ???????? console.php???????? 控制台应用配置 ???????? main.php??????????? Web 应用配置 ???????? test.php??????????? 功能测试使用的配置 ????? controllers/?????????? 包含控制器的类文件 ???????? SiteController.php? 默认控制器的类文件 ????? data/????????????????? 包含示例数据库 ???????? schema.mysql.sql??? 示例 MySQL 数据库 ???????? schema.sqlite.sql?? 示例 SQLite 数据库 ???????? testdrive.db??????? 示例 SQLite 数据库文件 ????? extensions/??????????? 包含第三方扩展 ????? messages/????????????? 包含翻译过的消息 ????? models/??????????????? 包含模型的类文件 ???????? LoginForm.php?????? 'login' 动作的表单模型 ???????? ContactForm.php???? 'contact' 动作的表单模型 ????? runtime/?????????????? 包含临时生成的文件 ????? tests/???????????????? 包含测试脚本 ????? views/???????????????? 包含控制器的视图和布局文件 ???????? layouts/??????????? 包含布局视图文件 ??????????? main.php???????? 所有视图的默认布局 ??????????? column1.php????? 使用单列页面使用的布局 ??????????? column2.php????? 使用双列的页面使用的布局 ???????? site/?????????????? 包含 'site' 控制器的视图文件 ??????????? pages/?????????? 包含 "静态" 页面 ?????????????? about.php???? "about" 页面的视图 ??????????? contact.php????? 'contact' 动作的视图 ??????????? error.php??????? 'error' 动作的视图(显示外部错误) ??????????? index.php??????? 'index' 动作的视图 ??????????? login.php??????? 'login' 动作的视图 ???????? system/???????????? 包含系统视图文件 这时不用写一行代码,我们就可以在浏览器中拜访如下 URL 来看看我们第一个 Yii 应用: http://hostname/testdrive/index.php 配置 在这个应用中,不管到那个页面url中都带有index.php,如果想把它去失落,怎么办. 1. 开启apache的mod_rewrite模块,去失落LoadModule rewrite_module modules/mod_rewrite.so前的"#"符号,确保<Directory "..."></Directory>中有"AllowOverride All".
代码如下:
'components'=>array( ?????????? ... ?????????? 'urlManager'=>array( ???????????????? 'urlFormat'=>'path', ???????????????? 'showScriptName'=>false,//注意false不要用引号括上 ???????????????? 'rules'=>array( ???????????????????? 'sites'=>'site/index', ???????????????? ), ?????????? ), ?????????? ... ?????? ), 3.配置服务器,Yii可以在Apache和Nginx下配置 1)Apache 在Apache服务器下,Yii必要配置.htaccess文件.配置如下
代码如下:
RewriteEngine on # prevent httpd from serving dotfiles (.htaccess,.svn,.git,etc.) RedirectMatch 403 /..*$ # if a directory or a file exists,use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php ?2)Nginx Yii可以使用Nginx和PHP的FPM SAPI.配置如下
代码如下:
server { ??? set $host_path "/www/mysite"; ??? access_log? /www/mysite/log/access.log? main; ??? server_name? mysite; ??? root?? $host_path/htdocs; ??? set $yii_bootstrap "index.php"; ??? charset utf-8; ??? location / { ??????? index? index.html $yii_bootstrap; ??????? try_files $uri $uri/ /$yii_bootstrap?$args; ??? } ??? location ~ ^/(protected|framework|themes/w+/views) { ??????? deny? all; ??? } ??? #avoid processing of calls to unexisting static files by yii ??? location ~ .(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ { ??????? try_files $uri =404; ??? } ??? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 ??? # ??? location ~ .php { ??????? fastcgi_split_path_info? ^(.+.php)(.*)$; ??????? #let yii catch the calls to unexising PHP files ??????? set $fsn /$yii_bootstrap; ??????? if (-f $document_root$fastcgi_script_name){ ??????????? set $fsn $fastcgi_script_name; ??????? } ??????? fastcgi_pass?? 127.0.0.1:9000; ??????? include fastcgi_params; ??????? fastcgi_param? SCRIPT_FILENAME? $document_root$fsn; ??????? #PATH_INFO and PATH_TRANSLATED can be omitted,but RFC 3875 specifies them for CGI ??????? fastcgi_param? PATH_INFO??????? $fastcgi_path_info; ??????? fastcgi_param? PATH_TRANSLATED? $document_root$fsn; ??? } ??? # prevent nginx from serving dotfiles (.htaccess,etc.) ??? location ~ /. { ??????? deny all; ??????? access_log off; ??????? log_not_found off; ??? } } ?使用如上配置,你可以在php.ini中设置cgi.fix_pathinfo=0,这样可以避免许多不需要的系统的stat()调用. 根本安装和配置就到这里~~ 编程之家培训学院每天发布《:Yii学习总结之安装配置》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |