Home  >  Article  >  Backend Development  >  Learning Yii (1)--Installation and Configuration_PHP Tutorial

Learning Yii (1)--Installation and Configuration_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:11928browse

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

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

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>

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 6e3512f89a97c220f19675c12d5a1a4cbb15ed4aadeed04b3991578461de0768 ". 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>,

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>

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>

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