|
I have previously written Yii articles on sina's blog. I haven't written any more articles about Yii since I came to the blog Park. it's just that I have nothing to do with the Dragon Boat Festival holiday. I just combined my previous blog and Yii official documents, in addition to the recent Yii gains, write a series ~~
Yii is a component-based high-performance PHP framework for developing large Web applications. Yii is written in strict OOP mode and has complete library references and Comprehensive tutorials. From MVC, DAO/ActiveRecord, widgets, caching, hierarchical RBAC, Web services, to topic, I18N and L10N, Yii provides almost all the functions required for today's Web 2.0 application development. In fact, Yii is one of the most efficient PHP frameworks. Yii is a high-performance PHP 5 web application development framework. Using a simple command line tool yiic, you 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 YiiBefore installing Yii, you must configure your development environment, such as a Web server that supports PHP5.1.0 or later. Yii has passed the Apache Web server test on Windows and Linux operating systems. It may also run on Web servers supporting PHP5 on other platforms. many free resources are published on the Internet, and you may get a Web server environment with PHP5 configured. Here we will leave the Web server and PHP5 installation aside. The installation of Yii is actually very simple. it takes only two steps:
- Extract the downloaded file from the http://www.yiiframework.com/download Yii Framework to a directory accessible to the Web server.
- After the installation is complete, we recommend that you check whether the current server meets all Yii requirements.
Fortunately, Yii comes with a simple check tool. To call it, enter http: // yourhostname/path/to/yii/requirements/index. php in your browser's address bar. the configuration of your server is shown below. Use the check tool to make sure that the server does not install and use extensions or components, but it only provides a suggestion to ensure that the installation is confirmed. As you can see, the following check results are not all in the Passed (pass) status, but some of them also display the Warning ). Of course, your configuration may be slightly different, so your display results may also be different. In fact, there is no need to pass all the details below. But it is also necessary. according to the Conclusion section, your server configuration meets the minimum requirements of Yii. (Your server configuration satisfies the minimum requirements by Yii .)Create a new application
- Yii installation location is already known to you
- WebRoot is the root directory configured on your Web server
- Go to the framework directory from your command line and execute the following content:
- % 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
Your application has been successfully created under/WebRoot/demo. This webapp command is used to create a brand new Yii application. It only needs to specify a parameter, whether it is an absolute or relative path, the application will be created. Its generated directories and files are only a skeleton of the application.
- Testdrive/
- Index. php Web application portal script file
- Portal script file used for index-test.php function testing
- Assets/contains public resource files
- Css/contains CSS files
- Images/contains Image files
- Themes/contains the application topic
- Protected/contains protected application files
- Yiic command line script
- Yiic. bat command line script in Windows
- Yiic. php yiic command line PHP script
- Commands/include the custom 'yic' command
- Shell/include the custom 'yicshell' command
- Components/contains reusable user components
- Basic classes of all Controller classes in Controller. php
- Identity. php is used to authenticate the 'Identity 'class.
- Config/contains the configuration file
- Console. php console application configuration
- Main. php Web application configuration
- Configuration used for test. php function testing
- Controllers/class files containing controllers
- SiteController. php default controller class file
- Data/contains the sample database
- Schema. mysql. SQL example MySQL database
- Schema. sqlite. SQL example SQLite database
- Testdrive. db sample SQLite database file
- Extensions/contains third-party extensions
- Messages/contains translated messages
- Models/class file containing the model
- Form model of LoginForm. php 'login' action
- Form model of ContactForm. php 'contact' action
- Runtime/contains temporary files
- Tests/contains test scripts
- Views/View and layout file containing the controller
- Layouts/contains Layout view files
- Default layout of all views in main. php
- Column1.php uses the layout of a single column page
- Column2.php layout for double-row pages
- Site/view file containing the 'site' controller
- Pages/contains "static" pages
- View of the about. php "about" page
- View of the contact. php 'contact' action
- Error. php 'error' action View (displaying external errors)
- View of index. php 'index' action
- Login. php 'login' action View
- System/contains the system view file
In this case, you do not need to write a line of code. we can access the following URL in the browser to see our first Yii application:
- Http: // hostname/testdrive/index. php
We can see that this application contains three pages: Homepage, contact page, and logon page. The home page displays information about applications and user logon statuses. the contact page displays a contact form for users to enter and submit their inquiries, the logon page allows users to pass authentication and then access authorized content. Configuration
In this application, no matter which page url contains index. php, what should I do if I want to remove it. 1. enable the mod_rewrite module of apache and remove the "#" symbol before LoadModule rewrite_module modules/mod_rewrite.so to ensure There is "AllowOverride All ". 2. add the code to/protected/config/main. php in the project:
- 'Components' => array (
- ...
- 'Urlmanager' => array (
- 'Urlformat' => 'path ',
- 'Showscriptname' => false, // note that false is not enclosed in quotation marks.
- 'Rules' => array (
- 'Sites '=> 'site/Index ',
- ),
- ),
- ...
- ),
3. configure the server. Yii can be configured in Apache and Nginx.
1) Apache
On the Apache server, configure the. htaccess file for Yii. 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
2) Nginx
Yii can use fpm sapi of Nginx and PHP. 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 // $ yii_bootstrap? $ Args;
- }
- Location ~ ^/(Protected | framework | themes/\ w +/views ){
- Deny all;
- }
- # Avoid processing of callto 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 Fig: 9000
- #
- Location ~ \. Php {
- Fastcgi_split_path_info ^ (. + \. php) (. *) $;
- # Let yii catch the callto 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,. svn,. git, etc .)
- Location ~ /\.{
- Deny all;
- Access_log off;
- Log_not_found off;
- }
- }
With the above configuration, you can set cgi. fix_pathinfo = 0 in php. ini to avoid many unnecessary system stat () calls.
Here is the basic installation and configuration ~~
|