extensions.md 4.28 KB
Newer Older
1 2 3
Extending Yii
=============

4 5 6
Code style
----------

7 8 9 10
- Use [core framework code style](https://github.com/yiisoft/yii2/wiki/Core-framework-code-style).
- Document classes, methods and properties using phpdoc. Note that you can use markdown and link to properties and methods
  using the following syntax: e.g. `[[name()]]`, `[[name\space\MyClass::name()]]`.
- Extension classes should not be prefixed. No `TbNavBar`, `EMyWidget`, etc.
11

12
### Namespace
13

14 15
- Do not use `yiisoft` in the namespaces.
- Do not use `\yii`, `\yii2` or `\yiisoft` as root namespace.
16
- Use `vendor-name` as the root namespace.
17 18 19 20 21 22 23 24

Distribution
------------

- There should be a `readme.md` file clearly describing what extension does in English, its requirements, how to install
  and use it. It should be written using markdown. If you want to provide translated readme, name it as `readme_ru.md`
  where `ru` is your language code. If extension provides a widget it is a good idea to include some screenshots.
- It is recommended to host your extensions at [Github](github.com).
25 26
- Extension should be registered at [Packagist](https://packagist.org) in order to be installable via Composer.
  Choose package name wisely since changing it leads to losing stats and inability to install package by the old name.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

### Composer package name

If your extension was made specifically for Yii2 (i.e. cannot be used as a standalone PHP library) it is recommended to
name it like the following:

```
yii2-my-extension-name-type
```

In the above:

- `yii2-` prefix.
- Extension name lowecase, words separated by `-`.
- `-type` postfix where type may be `widget`, `behavior`, `module` etc.
42

43 44
### Dependencies

45 46 47
- Additional code, eg. libraries, should be required in your `composer.json` file.
- When extension is released in a stable version, its requirements should not include `dev` packages that do not
  have a `stable` release.
48 49 50 51
- Use appropriate version constraints, eg. `1.*`, `@stable` for requirements.

### Versioning

52 53 54
- Use the rules of [semantic versioning](http://semver.org).
- Use a consistent format for your repository tags, as they are treated as version strings by composer, eg. `0.2.4`,
  `0.2.5`,`0.3.0`,`1.0.0`.
55

56 57
### composer.json

58 59 60
- Use the type `yii2-extension` in `composer.json` file if your extension is Yii-specific.
- Do not use `yii` or `yii2` as composer vendor name.
- Do not use `yiisoft` in the composer package name or the composer vendor name.
61

62 63 64 65
If your extension classes reside directly in repository root use PSR-4 the following way in your `composer.json`:

```
{
66 67 68 69
        "name": "myname/mywidget",
        "description": "My widget is a cool widget that does everything",
        "keywords": ["yii", "extension", "widget", "cool"],
        "homepage": "https://github.com/myname/yii2-mywidget-widget",
70 71 72 73
        "type": "yii2-extension",
        "license": "BSD-3-Clause",
        "authors": [
                {
74 75
                        "name": "John Doe",
                        "email": "doe@example.com"
76 77 78 79 80 81 82
                }
        ],
        "require": {
                "yiisoft/yii2": "*"
        },
        "autoload": {
                "psr-4": {
83
                        "myname\\mywidget\\": ""
84 85 86 87 88
                }
        }
}
```

89
In the above `myname/mywidget` is the package name that will be registered
90
at [Packagist](https://packagist.org). It is common for it to match your github repository.
91

92
We're using `psr-4` autoloader and mapping `myname\mywidget` namespace to the root directory where our classes reside.
93 94

More details can be found in the [composer documentation](http://getcomposer.org/doc/04-schema.md#autoload).
95 96 97 98 99

Working with database
---------------------

- If extension creates or modifies database schema always use Yii migrations instead of SQL files or custom scripts.
100 101
- Migrations should be appliable to as many data storages as possible.
- Do not use active record models in your migrations.
102 103 104 105

Assets
------

106
- Register assets [through bundles](assets.md).
107 108 109 110

Events
------

Tobias Munk committed
111
TBD
112 113 114 115

i18n
----

116 117 118
- If extension outputs messages intended for end user these should be wrapped into `Yii::t()` in order to be translatable.
- Exceptions and other developer-oriented message should not be translated.
- Consider proving `config.php` for `yii message` command to simplify translation.
119 120 121 122

Testing your extension
----------------------

123
- Consider adding unit tests for PHPUnit.