Pages

Tuesday, November 11, 2014

Yii 2.0 Application Structure

The most important directories and files in your application are (assuming the application's root directory is basic):

basic/                  application base path
    composer.json       used by Composer, describes package information
    config/             contains application and other configurations
        console.php     the console application configuration
        web.php         the Web application configuration
    commands/           contains console command classes
    controllers/        contains controller classes
    models/             contains model classes
    runtime/            contains files generated by Yii during runtime, such as logs and cache files
    vendor/             contains the installed Composer packages, including the Yii framework itself
    views/              contains view files
    web/                application Web root, contains Web accessible files
        assets/         contains published asset files (javascript and css) by Yii
        index.php       the entry (or bootstrap) script for the application
    yii                 the Yii console command execution script
In general, the files in the application can be divided into two types: those under basic/web and those under other directories. The former can be directly accessed via HTTP (i.e., in a browser), while the latter can not and should not be.

Yii implements the model-view-controller (MVC) design pattern, which is reflected in the above directory organization. The models directory contains all model classes, the views directory contains all view scripts, and the controllers directory contains all controller classes.

The following diagram shows the static structure of an application.
Each application has an entry script web/index.php which is the only Web accessible PHP script in the application. The entry script takes an incoming request and creates an application instance to handle it. The application resolves the request with the help of its components, and dispatches the request to the MVC elements. Widgets are used in the views to help build complex and dynamic user interface elements.

Saturday, October 25, 2014

Configure your Production Web Server (Yii 2.0)

Note: If you are only test driving Yii, you may not do this to your development server.

The application installed according to the above instructions should work out of box with either an Apache HTTP server or an Nginx HTTP server, on Windows, Mac OS X, or Linux running PHP 5.4 or higher. Yii 2.0 is also compatible with facebooks HHVM however there are some edge cases where HHVM behaves different than native PHP so you have to take some extra care when using HHVM.

On a production server, you may want to configure your Web server so that the application can be accessed via the URLhttp://www.example.com/index.php instead of http://www.example.com/basic/web/index.php. Such configuration requires pointing the document root of your Web server to the basic/web folder. You may also want to hide index.php from the URL.

Note: By setting basic/web as the document root, you also prevent end users from accessing your private application code and sensitive data files that are stored in the sibling directories of basic/web. Denying access to those other folders is a security improvement.


Recommended Apache Configuration

Use the following configuration in Apache's httpd.conf file or within a virtual host configuration. Note that you should replace path/to/basic/web with the actual path for basic/web.
# Set document root to be "basic/web"
DocumentRoot "path/to/basic/web"

<Directory "path/to/basic/web">
    # use mod_rewrite for pretty URL support
    RewriteEngine on
    # If a directory or a file exists, use the request directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Otherwise forward the request to index.php
    RewriteRule . index.php

    # ...other settings...
</Directory>

Recommended Nginx Configuration

You should have installed PHP as an FPM SAPI to use Nginx. Use the following Nginx configuration, replacing path/to/basic/web with the actual path for basic/web and mysite.local with the actual hostname to serve.
server {
    charset utf-8;
    client_max_body_size 128M;

    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name mysite.local;
    root        /path/to/basic/web;
    index       index.php;

    access_log  /path/to/basic/log/access.log main;
    error_log   /path/to/basic/log/error.log;

    location / {
        # Redirect everything that isn't a real file to index.php
        try_files $uri $uri/ /index.php?$args;
    }

    # uncomment to avoid processing of calls to non-existing static files by Yii
    #location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
    #    try_files $uri =404;
    #}
    #error_page 404 /404.html;

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        try_files $uri =404;
    }

    location ~ /\.(ht|svn|git) {
        deny all;
    }
}
When using this configuration, you should also set cgi.fix_pathinfo=0 in the php.ini file in order to avoid many unnecessary system stat() calls.
Also note that when running an HTTPS server, you need to add fastcgi_param HTTPS on; so that Yii can properly detect if a connection is secure.

Tuesday, October 21, 2014

Verify your Yii 2.0 Installation

After installation, you can use your browser to access the installed Yii application with the following URL:

http://localhost/basic/web/index.php

Yii 2.0.0 is finally released

Yii 2.0 is finally coming, after more than three years of intensive development with almost 10,000 commits by over 300 authors.

Yii 2.0 is a complete rewrite over the previous version 1.1. They have made this choice in order to build a state-of-the-art PHP framework by keeping the original simplicity and extensibility of Yii while adopting the latest technologies and features to make it even better. Finally, this is now a reality!!! I have been waiting for the release of Yii 2.0, and I can not wait to upgrade all my web applications to the 2.0 version!