All notes
Yii

Getting Started

Installation

# global: Installs packages (e.g. vendor directory) in ~/.composer.
# require: Adds required packages to your composer.json and installs them.
composer.phar global require "fxp/composer-asset-plugin:~1.1.1"
composer.phar require yiisoft/yii2

Hello world

http://localhost:8080/index.php?r=site/hello&msg=wcf

Key Concepts

Components

Components are the main building blocks of Yii applications. Components are instances of yii\base\Component or a derived class. Components provide three main features: Properties, Events, and Behaviors.

Components are heavier than Objects. If you do not need support event and behavior, then use Objects yii\base\Object instead (with support of property).

Guidelines

When extending your class from yii\base\Component or yii\base\Object:

<?php
namespace yii\components\MyClass;

use yii\base\Object;

class MyClass extends Object
{
    public $prop1;
    public $prop2;

    // Put config at the last.
    public function __construct($param1, $param2, $config = [])
    {
        // ... initialization before configuration is applied

        parent::__construct($config); // Call parent constructor in the end.
    }

    public function init()
    {
        parent::init(); // Call parent init() in the beginning.

        // ... initialization after configuration is applied
    }
}

// Following these guidelines will make your components configurable when they are created. For example:

// Direct call:
$component = new MyClass(1, 2, ['prop1' => 3, 'prop2' => 4]);

// Alternatively:
// while it looks more complicated, it is more powerful because it is implemented on top of a dependency injection container.
$component = \Yii::createObject([
    'class' => MyClass::className(),
    'prop1' => 3,
    'prop2' => 4,
], [1, 2]);

The yii\base\Object class enforces the following initialization in the constructors:

  1. Pre-initialization within the constructor. You can set default property values here.
  2. Object configuration via \$config. The configuration may overwrite the default values set within the constructor.
  3. Post-initialization within init(). You may override this method to perform sanity checks and normalization of the properties.

Property

Usually, class member variables are also called properties. However, when we want to read and write on the properties in special ways, such as always trimming:

$object->label = trim($label);

Yii introduces a base class called yii\base\Object that supports defining properties based on getter and setter class methods.

namespace app\components;

use yii\base\Object;

class Foo extends Object
{
    // Properties always start with '_' and is private.
    // Then yii will synthesize a public accessible method 'label'. See below.
    private $_label;

    public function getLabel()
    {
        return $this->_label;
    }

    public function setLabel($value)
    {
        $this->_label = trim($value);
    }
}

// Use Properties:

// equivalent to $label = $object->getLabel();
$label = $object->label;
// equivalent to $object->setLabel('abc');
$object->label = 'abc';

Rules