search
HomeBackend DevelopmentPHP TutorialYii learning (1)--Installation and configuration

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

  1. testdrive/
  2. index.php Web application entry script file
  3. index-test.php entry script file used for functional testing
  4. assets/ contains public resource files
  5. css/ contains CSS files
  6. images/ contains image files
  7. / Contains application themes
  8. protected/ Contains protected application files
  9. yiic yiic command line script
  10. yiic.bat yiic command line script under Windows
  11. yiic.php yiic command line PHP script
  12. commands/ Contains custom 'yiic' commands
  13. shell/ contains custom 'yiic shell' commands
  14. components/ contains reusable user components
  15. Controller.php is the base class for all controller classes
  16. Identity.php is the 'Identity' class used for authentication
  17. config/ contains configuration files
  18. console.php Console application configuration
  19. main.php Web application configuration
  20. test.php Configuration used for functional testing
  21. controllers/ Contains the class file of the controller
  22. SiteController.php The class file of the default controller
  23. data/ Contains the sample database
  24. schema.mysql.sql Example MySQL database
  25. schema.sqlite.sql Example SQLite database
  26. testdrive.db Example SQLite database file
  27. extensions/ Contains third-party extensions
  28. messages/ Contains translated messages
  29. models/ Contains class files for models
  30. LoginForm .php 'login' action form model
  31. ContactForm.php 'contact' action form model
  32. runtime/ contains temporarily generated files
  33. tests/ contains test scripts
  34. views/ contains controller view and layout files
  35. layouts/ contains layouts View file
  36. main.php The default layout for all views
  37. column1.php The layout used for single-column pages
  38. column2.php The layout used for pages using double columns
  39. site/ The view file that contains the 'site' controller
  40. pages/ contains " Static" page
  41. about.php "about" page view
  42. contact.php 'contact' action view
  43. error.php 'error' action view (shows external errors)
  44. index.php 'index' action view
  45. login .php 'login' action view
  46. 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:

  1. 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:
  1. 'components'=>array(
  2. ...
  3. 'urlManager'=>array(
  4. 'urlFormat'=>'path',
  5. 'showScriptName'=>false,//Note that false is not allowed Surround it with quotes
  6. 'rules'=>array(
  7. 'sites'=>'site/index',
  8. ),
  9. ),
  10. ...
  11. ),
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

  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
Copy code

2) Nginx

Yii can use Nginx and PHP’s FPM SAPI. 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 $uri/ /$yii_bootstrap?$args;
  11. }
  12. location ~ ^/(protected|framework| themes/w+/views) {
  13. deny all;
  14. }
  15. #avoid processing of calls to 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 127.0.0.1:9000
  20. #
  21. location ~ .php {
  22. fastcgi_split_path_info ^(.+.php) (.*)$;
  23. #let yii catch the calls to 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. fastc gi_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. }
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~~



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools