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, Yii’s official documents, and the recent about Summarize the gains from 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 has met all the requirements of Yii.
Fortunately, doing this is easy and Yii comes with a simple inspection tool. To call it, enter: http://yourhostname/path/to/yii/requirements/index.php in your browser address bar. Your server's configuration will be displayed below. Use the check tool to determine that the server does not have extensions or components installed and used, but it only gives a suggestion to ensure that the installation can be determined. As you can see, not all of the following check results are in Passed status, some also show Warning. Of course, your configuration may be slightly different, and therefore, your display results will be different. In fact, it is not necessary to pass all the details below. But part of it is also necessary. According to the content of the Conclusion paragraph: your server configuration meets the minimum requirements of Yii. (Your server configuration satisfies the minimum requirements by Yii.)
Create a new app
- The installation location of Yii is something you already know
- WebRoot is the root directory of your web server configuration
- From your command line, go to the framework directory and execute the following:
- % 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
Copy code
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/
- index.php Web application entry script file
- index-test.php entry script file used for functional testing
- assets/ contains public resource files
- css/ contains CSS files
- images/ contains image files
- / Contains application themes
- protected/ Contains protected application files
- yiic yiic command line script
- yiic.bat yiic command line script under Windows
- yiic.php yiic command line PHP script
- commands/ Contains custom 'yiic' commands
- shell/ contains custom 'yiic shell' commands
- components/ contains reusable user components
- Controller.php is the base class for all controller classes
- Identity.php is the 'Identity' class used for authentication
- config/ contains configuration files
- console.php Console application configuration
- main.php Web application configuration
- test.php Configuration used for functional testing
- controllers/ Contains the class file of the controller
- SiteController.php The class file of the default controller
- data/ Contains the sample database
- schema.mysql.sql Example MySQL database
- schema.sqlite.sql Example SQLite database
- testdrive.db Example SQLite database file
- extensions/ Contains third-party extensions
- messages/ Contains translated messages
- models/ Contains class files for models
- LoginForm .php 'login' action form model
- ContactForm.php 'contact' action form model
- runtime/ contains temporarily generated files
- tests/ contains test scripts
- views/ contains controller view and layout files
- layouts/ contains layouts View file
- main.php The default layout for all views
- column1.php The layout used for single-column pages
- column2.php The layout used for pages using double columns
- site/ The view file that contains the 'site' controller
- pages/ contains " Static" page
- about.php "about" page view
- contact.php 'contact' action view
- error.php 'error' action view (shows external errors)
- index.php 'index' action view
- login .php 'login' action view
- system/ contains system view files
Copy code
At this time, without writing a line of code, we can access the following URL in the browser to see our first Yii application:
- http://hostname/testdrive/index.php
Copy code
As we will see, this application contains three pages: homepage, contact page, and login page. The home page 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 .
2. Add code to /protected/config/main.php in the project:
- 'components'=>array(
- ...
- 'urlManager'=>array(
- 'urlFormat'=>'path',
- 'showScriptName'=>false,//Note that false is not allowed Surround it with quotes
- 'rules'=>array(
- 'sites'=>'site/index',
- ),
- ),
- ...
- ),
Copy code
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
- 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
Copy code
2) Nginx
Yii can use Nginx and PHP’s FPM SAPI. The configuration is as follows
- 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;
- fastc gi_param PATH_TRANSLATED $document_root$fsn;
- }
-
- # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
- location ~ /. {
- deny all;
- access_log off;
- log_not_found off;
- }
- }
Copy code
Using the above configuration, you can set cgi.fix_pathinfo=0 in php.ini, which can avoid many unnecessary system stat() calls.
Basic installation and configuration ends here~~
|