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.
Install Yii
Before installing Yii, you must configure your development environment, such as a web server that supports PHP5.1.0 or above. Yii has been tested on the Apache web server on Windows and Linux operating systems. It may also run on web servers that support PHP5 on other platforms. There are many free resources published on the Internet, and you may get a web server environment configured with PHP5. Here we will leave aside the web server and PHP5 installation. The installation of Yii is actually very simple. It only requires two steps:- Download Yii framework from http://www.yiiframework.com/ Unzip the downloaded file to a directory accessible to the web server.
- After the installation is completed, it is recommended that you check whether the current server meets all the requirements of Yii.
Create a new application
- You already know the installation location of Yii
- WebRoot is the root directory of your web server configuration
- From your command line, go to the framework directory and execute the following:
%<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.
Configuration
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'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~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
