Home >Backend Development >PHP Tutorial >Learning Yii (1)--Installation and Configuration_PHP Tutorial
I have written articles about Yii on the Sina blog before. After coming to the blog park, I have not written any articles about Yii. It happened that I had nothing to do during the Dragon Boat Festival holiday, so I combined the previous blog and the official documentation of Yii. Add a summary of the recent gains about Yii and write a series~~
Yii is a high-performance component-based PHP framework for developing large-scale web applications. Yii is written in strict OOP and has complete library references and comprehensive tutorials. From MVC, DAO/ActiveRecord, widgets, caching, hierarchical RBAC, web services, to theming, I18N and L10N, Yii provides almost everything needed for today's Web 2.0 application development. In fact, Yii is one of the most efficient PHP frameworks. Yii is a high-performance PHP5 web application development framework. A simple command line tool yiic can quickly create a web application code framework. Developers can add business logic based on the generated code framework to quickly complete application development.
%<span> cd Webroot/testdrive/framework </span>% <span>yiic webapp ../../testdrive Create a Web application under </span><span>'</span><span>/WebRoot/testdrive</span><span>'</span>? [Yes|<span>No] Yes </span><span>mkdir</span> /WebRoot/<span>testdrive </span><span>mkdir</span> /WebRoot/testdrive/<span>assets </span><span>mkdir</span> /WebRoot/testdrive/<span>css generate css</span>/<span>bg.gif generate css</span>/<span>form.css generate css</span>/main.css
Your application has been successfully created under /WebRoot/demo. The purpose of this webapp command is to create a brand new Yii application. It only requires specifying a parameter, whether it is an absolute or relative path and the application will be created. The directories and files it generates are just a skeleton of the application.
testdrive/<span> index.php Web 应用入口脚本文件 index</span>-<span>test.php 功能测试使用的入口脚本文件 assets</span>/<span> 包含公开的资源文件 css</span>/<span> 包含 CSS 文件 images</span>/<span> 包含图片文件 themes</span>/<span> 包含应用主题 protected</span>/<span> 包含受保护的应用文件 yiic yiic 命令行脚本 yiic.bat Windows 下的 yiic 命令行脚本 yiic.php yiic 命令行 PHP 脚本 commands</span>/ 包含自定义的 <span>'</span><span>yiic</span><span>'</span><span> 命令 shell</span>/ 包含自定义的 <span>'</span><span>yiic shell</span><span>'</span><span> 命令 components</span>/<span> 包含可重用的用户组件 Controller.php 所有控制器类的基础类 Identity.php 用来认证的 </span><span>'</span><span>Identity</span><span>'</span><span> 类 config</span>/<span> 包含配置文件 console.php 控制台应用配置 main.php Web 应用配置 test.php 功能测试使用的配置 controllers</span>/<span> 包含控制器的类文件 SiteController.php 默认控制器的类文件 data</span>/<span> 包含示例数据库 schema.mysql.sql 示例 MySQL 数据库 schema.sqlite.sql 示例 SQLite 数据库 testdrive.db 示例 SQLite 数据库文件 extensions</span>/<span> 包含第三方扩展 messages</span>/<span> 包含翻译过的消息 models</span>/<span> 包含模型的类文件 LoginForm.php </span><span>'</span><span>login</span><span>'</span><span> 动作的表单模型 ContactForm.php </span><span>'</span><span>contact</span><span>'</span><span> 动作的表单模型 runtime</span>/<span> 包含临时生成的文件 tests</span>/<span> 包含测试脚本 views</span>/<span> 包含控制器的视图和布局文件 layouts</span>/<span> 包含布局视图文件 main.php 所有视图的默认布局 column1.php 使用单列页面使用的布局 column2.php 使用双列的页面使用的布局 site</span>/ 包含 <span>'</span><span>site</span><span>'</span><span> 控制器的视图文件 pages</span>/ 包含 <span>"</span><span>静态</span><span>"</span><span> 页面 about.php </span><span>"</span><span>about</span><span>"</span><span> 页面的视图 contact.php </span><span>'</span><span>contact</span><span>'</span><span> 动作的视图 error.php </span><span>'</span><span>error</span><span>'</span><span> 动作的视图(显示外部错误) index.php </span><span>'</span><span>index</span><span>'</span><span> 动作的视图 </span><span>login</span>.php <span>'</span><span>login</span><span>'</span><span> 动作的视图 system</span>/ 包含系统视图文件
Without writing a line of code at this time, we can access the following URL in the browser to see our first Yii application:
http:<span>//</span><span>hostname/testdrive/index.php</span>
As we will see, this application contains three pages: home page, contact page, and login page. The homepage displays some information about the application and the user's login status, the contact page displays a contact form for users to fill out and submit their inquiries, and the login page allows users to first authenticate and then access authorized content.
In this application, no matter which page URL you go to, index.php is included. What should I do if I want to remove it?
1. Open apache's mod_rewrite module, remove the "#" symbol in front of LoadModule rewrite_module modules/mod_rewrite.so, and make sure there is "AllowOverride All" in 6e3512f89a97c220f19675c12d5a1a4cbb15ed4aadeed04b3991578461de0768 ". 2. Add code in /protected/config/main.php in the project:'components'=><span>array</span><span>( </span>... 'urlManager'=><span>array</span><span>( </span>'urlFormat'=>'path', 'showScriptName'=><span>false</span>,<span>//</span><span>注意false不要用引号括上</span> 'rules'=><span>array</span><span>( </span>'sites'=>'site/index',<span> )</span>,<span> )</span>, ...<span> )</span>,
3. Configure the server, Yii can be configured under Apache and Nginx
1) Apache
Under the Apache server, Yii needs to configure the .htaccess file. The configuration is as follows
<span>RewriteEngine on # prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.) RedirectMatch </span><span>403</span> /\..*<span>$ # </span><span>if</span> a directory or a <span>file</span><span> exists, use it directly RewriteCond </span>%{REQUEST_FILENAME} !-<span>f RewriteCond </span>%{REQUEST_FILENAME} !-<span>d # otherwise forward it to index.php RewriteRule . index.php</span>
2) Nginx
Yii can use Nginx and PHP's FPM SAPI. The configuration is as follows
<span>server { set $host_path </span><span>"</span><span>/www/mysite</span><span>"</span><span>; access_log </span>/www/mysite/log/<span>access.log main; server_name mysite; root $host_path</span>/<span>htdocs; set $yii_bootstrap </span><span>"</span><span>index.php</span><span>"</span><span>; charset utf</span>-<span>8</span><span>; location </span>/<span> { index index.html $yii_bootstrap; try_files $uri $uri</span>/ /$yii_bootstrap?<span>$args; } location </span>~ ^/(protected|framework|themes/\<span>w</span>+/<span>views) { deny all; } #avoid processing of calls to unexisting static files by yii location </span>~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|<span>zip</span>|<span>rar)$ { try_files $uri </span>=<span>404</span><span>; } # pass the PHP scripts to FastCGI server listening on </span><span>127.0</span>.<span>0.1</span>:<span>9000</span><span> # location </span>~<span> \.php { fastcgi_split_path_info </span>^(.+\.php)(.*<span>)$; #let yii catch the calls to unexising PHP files set $fsn </span>/<span>$yii_bootstrap; </span><span>if</span> (-<span>f $document_root$fastcgi_script_name){ set $fsn $fastcgi_script_name; } fastcgi_pass </span><span>127.0</span>.<span>0.1</span>:<span>9000</span><span>; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fsn; #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC </span><span>3875</span> specifies them <span>for</span><span> CGI fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fsn; } # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.) location </span>~ /<span>\. { deny all; access_log off; log_not_found off; } }</span>
Using the above configuration, you can set cgi.fix_pathinfo=0 in php.ini, which can avoid many unnecessary system's stat( ) call.
Basic installation and configuration ends here~~