# 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
- "r=site": render "site" controller, which is defined in controllers/SiteController.php.
- "hello" is the view, defined in views/site/hello.php, where "site" is the controller.
- The layout is under views/layouts/.
- An action actionHello should be added in controllers/SiteController.php.
- Controller ID post-comment corresponds to the controller class name PostCommentController.
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).
When extending your class from yii\base\Component or yii\base\Object:
- If you override the constructor, specify a \$config parameter as the constructor's last parameter, and then pass this parameter to the parent constructor.
- Always call the parent constructor at the end of your overriding constructor.
- If you override the yii\base\Object::init() method, make sure you call the parent implementation of init at the beginning of your init method.
class MyClass extends Object
// 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]);
// 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:
- Pre-initialization within the constructor. You can set default property values here.
- Object configuration via \$config. The configuration may overwrite the default values set within the constructor.
- Post-initialization within init(). You may override this method to perform sanity checks and normalization of the properties.
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.
class Foo extends Object
// Properties always start with '_' and is private.
// Then yii will synthesize a public accessible method 'label'. See below.
public function getLabel()
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';
- The names of such properties are case-insensitive. For example, \$object->label and \$object->Label are the same. This is because method names in PHP are case-insensitive.
- If the name of such a property is the same as a class member variable, the latter will take precedence.
- The properties can only be defined by non-static getters and/or setters. Static methods will not be treated in the same manner.
- A normal call to property_exists() does not work to determine magic properties. You should call canGetProperty() or canSetProperty() respectively.