Pages

Tuesday, October 21, 2014

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!


Be updated with Yii (click the links below):
  • Yii project site
  • Yii 2.0 GitHub Project
  • Yii Facebook group
  • Yii Twitter feeds
  • Yii LinkedIn group

  • Highlights

    Adopting Standards and Latest Technologies

    Yii 2.0 adopts PHP namespaces and traits, PSR standards, Composer and Bower. All these make the framework more refreshing and interoperable with other libraries.

    Solid Foundation Classes

    Like in 1.1, Yii 2.0 supports object properties defined via getters and setters, configurations, events and behaviors. The new implementation is more efficient and expressive. For example, you can write the following code to respond to an event:

    $response = new yii\web\Response;
    $response->on('beforeSend', function ($event) {
        // respond to the "beforeSend" event here
    });
    Yii 2.0 implements the dependency injection container and service locator. It makes the applications built with Yii more customizable and testable.

    Development Tools

    Yii 2.0 comes with several development tools to make the life of developers easier.

    The Yii debugger allows you to examine the runtime internals of your application. It can also be used to do performance profiling to find out the performance bottlenecks in your application.

    Like 1.1, Yii 2.0 also provides Gii, a code generation tool, that can cut down a large portion of your development time. Gii is very extensible, allowing you to customize or create different code generators. Gii provides both Web and console interfaces to fit for different user preferences.

    The API documentation of Yii 1.1 has received a lot of positive feedback. Many people expressed the wish to create a similar documentation for their applications. Yii 2.0 realizes this with a documentation generator. The generator supports Markdown syntax which allows you to write documentation in a more succinct and expressive fashion.

    Security

    Yii 2.0 helps you to write more secure code. It has built-in support to prevent SQL injections, XSS attacks, CSRF attacks, cookie tampering, etc.

    Databases

    Working with databases has never been easier. Yii 2.0 supports DB migration, database access objects (DAO), query builder and Active Record. Compared with 1.1, Yii 2.0 improves the performance of Active Record and unifies the syntax for querying data via query builder and Active Record. The following code shows how you can query customer data using either query builder or Active Record. As you can see, both approaches use chained method calls which are similar to SQL syntax.
    
    use yii\db\Query;
    use app\models\Customer;
     
    $customers = (new Query)->from('customer')
        ->where(['status' => Customer::STATUS_ACTIVE])
        ->orderBy('id')
        ->all();
     
    $customers = Customer::find()
        ->where(['status' => Customer::STATUS_ACTIVE])
        ->orderBy('id')
        ->asArray();
        ->all();
    The following code shows how you can perform relational queries using Active Record:
    namespace app\models;
     
    use app\models\Order;
    use yii\db\ActiveRecord;
     
    class Customer extends ActiveRecord
    {
        public static function tableName()
        {
            return 'customer';
        }
     
        // defines a one-to-many relation with Order model
        public function getOrders()
        {
            return $this->hasMany(Order::className(), ['customer_id' => 'id']);
        }
    }
     
    // returns the customer whose id is 100
    $customer = Customer::findOne(100);
    // returns the orders for the customer
    $orders = $customer->orders;
    And the following code shows how you can update a Customer record. Behind the scene, parameter binding is used to prevent SQL injection attacks, and only modified columns are saved to DB.
    $customer = Customer::findOne(100);
    $customer->address = '123 Anderson St';
    $customer->save();  // executes SQL: UPDATE `customer` SET `address`='123 Anderson St' WHERE `id`=100
    Yii 2.0 supports the widest range of databases. Besides the traditional relational databases, Yii 2.0 adds the support for Cubrid, ElasticSearch, Sphinx. It also supports NoSQL databases, including Redis and MongoDB. More importantly, the same query builder and Active Record APIs can be used for all these databases, which makes it an easy task for you to switch among different databases. And when using Active Record, you can even relate data from different databases (e.g. between MySQL and Redis).
    
    For applications with big databases and high performance requirement, Yii 2.0 also provides built-in support for database replication and read-write splitting.

    No comments:

    Post a Comment