Table of Contents
Install Yii
Create a new application
Configuration
Home Backend Development PHP Tutorial Learning Yii (1)--Installation and Configuration_PHP Tutorial

Learning Yii (1)--Installation and Configuration_PHP Tutorial

Jul 13, 2016 am 10:29 AM
php yii

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.
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. The following will display the configuration of your server. 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, but 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 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
Copy after login

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>/             包含系统视图文件
Copy after login

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>
Copy after login

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 ". 2. Add code in /protected/config/main.php in the project:
'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>,
Copy after login

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>
Copy after login

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>
Copy after login

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/778932.htmlTechArticleI have written articles about Yii on the sina blog before. After coming to the blog park, I have not written any articles about Yii. , I just have nothing to do during the Dragon Boat Festival holiday, so I combined my previous blog, Yii’s official documentation, and...
Statement of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles