Yii learning (1) -- install and configure

Source: Internet
Author: User
Yii learning (1) -- install and configure

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:

  1. % Cd Webroot/testdrive/framework
  2. % Yiic webapp.../../testdrive
  3. Create a Web application under '/WebRoot/testdrive '? [Yes | No]
  4. Yes
  5. Mkdir/WebRoot/testdrive
  6. Mkdir/WebRoot/testdrive/assets
  7. Mkdir/WebRoot/testdrive/css
  8. Generate css/bg.gif
  9. Generate css/form.css
  10. 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.

  1. Testdrive/
  2. Index. php Web application portal script file
  3. Portal script file used for index-test.php function testing
  4. Assets/contains public resource files
  5. Css/contains CSS files
  6. Images/contains Image files
  7. Themes/contains the application topic
  8. Protected/contains protected application files
  9. Yiic command line script
  10. Yiic. bat command line script in Windows
  11. Yiic. php yiic command line PHP script
  12. Commands/include the custom 'yic' command
  13. Shell/include the custom 'yicshell' command
  14. Components/contains reusable user components
  15. Basic classes of all Controller classes in Controller. php
  16. Identity. php is used to authenticate the 'Identity 'class.
  17. Config/contains the configuration file
  18. Console. php console application configuration
  19. Main. php Web application configuration
  20. Configuration used for test. php function testing
  21. Controllers/class files containing controllers
  22. SiteController. php default controller class file
  23. Data/contains the sample database
  24. Schema. mysql. SQL example MySQL database
  25. Schema. sqlite. SQL example SQLite database
  26. Testdrive. db sample SQLite database file
  27. Extensions/contains third-party extensions
  28. Messages/contains translated messages
  29. Models/class file containing the model
  30. Form model of LoginForm. php 'login' action
  31. Form model of ContactForm. php 'contact' action
  32. Runtime/contains temporary files
  33. Tests/contains test scripts
  34. Views/View and layout file containing the controller
  35. Layouts/contains Layout view files
  36. Default layout of all views in main. php
  37. Column1.php uses the layout of a single column page
  38. Column2.php layout for double-row pages
  39. Site/view file containing the 'site' controller
  40. Pages/contains "static" pages
  41. View of the about. php "about" page
  42. View of the contact. php 'contact' action
  43. Error. php 'error' action View (displaying external errors)
  44. View of index. php 'index' action
  45. Login. php 'login' action View
  46. 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:

  1. 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:

  1. 'Components' => array (
  2. ...
  3. 'Urlmanager' => array (
  4. 'Urlformat' => 'path ',
  5. 'Showscriptname' => false, // note that false is not enclosed in quotation marks.
  6. 'Rules' => array (
  7. 'Sites '=> 'site/Index ',
  8. ),
  9. ),
  10. ...
  11. ),

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:

  1. RewriteEngine on
  2. # Prevent httpd from serving dotfiles (. htaccess,. svn,. git, etc .)
  3. RedirectMatch 403/\... * $
  4. # If a directory or a file exists, use it directly
  5. RewriteCond % {REQUEST_FILENAME }! -F
  6. RewriteCond % {REQUEST_FILENAME }! -D
  7. # Otherwise forward it to index. php
  8. RewriteRule. index. php

2) Nginx

Yii can use fpm sapi of Nginx and PHP. The configuration is as follows:

  1. Server {
  2. Set $ host_path "/www/mysite ";
  3. Access_log/www/mysite/log/access. log main;
  4. Server_name mysite;
  5. Root $ host_path/htdocs;
  6. Set $ yii_bootstrap "index. php ";
  7. Charset UTF-8;
  8. Location /{
  9. Index index.html $ yii_bootstrap;
  10. Try_files $ uri // $ yii_bootstrap? $ Args;
  11. }
  12. Location ~ ^/(Protected | framework | themes/\ w +/views ){
  13. Deny all;
  14. }
  15. # Avoid processing of callto unexisting static files by yii
  16. Location ~ \. (Js | css | png | jpg | gif | swf | ico | pdf | mov | fla | zip | rar) $ {
  17. Try_files $ uri = 404;
  18. }
  19. # Pass the PHP scripts to FastCGI server listening on Fig: 9000
  20. #
  21. Location ~ \. Php {
  22. Fastcgi_split_path_info ^ (. + \. php) (. *) $;
  23. # Let yii catch the callto unexising PHP files
  24. Set $ fsn/$ yii_bootstrap;
  25. If (-f $ document_root $ fastcgi_script_name ){
  26. Set $ fsn $ fastcgi_script_name;
  27. }
  28. Fastcgi_pass 127.0.0.1: 9000;
  29. Include fastcgi_params;
  30. Fastcgi_param SCRIPT_FILENAME $ document_root $ fsn;
  31. # PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
  32. Fastcgi_param PATH_INFO $ fastcgi_path_info;
  33. Fastcgi_param PATH_TRANSLATED $ document_root $ fsn;
  34. }
  35. # Prevent nginx from serving dotfiles (. htaccess,. svn,. git, etc .)
  36. Location ~ /\.{
  37. Deny all;
  38. Access_log off;
  39. Log_not_found off;
  40. }
  41. }

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 ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.