apps-advanced.md 8.01 KB
Newer Older
1 2 3
Advanced application template
=============================

4 5 6 7
This template is for large projects developed in teams where backend is divided from frontend, application is deployed
to multiple servers etc. This application template also goes a bit further regarding features and provides essential
database, signup and password restore out of the box.

Alexander Makarov committed
8 9 10
Installation
------------

11 12 13 14 15 16 17 18 19 20 21 22
### Install via Composer

If you do not have [Composer](http://getcomposer.org/), you may download it from
[http://getcomposer.org/](http://getcomposer.org/) or run the following command on Linux/Unix/MacOS:

~~~
curl -s http://getcomposer.org/installer | php
~~~

You can then install the application using the following command:

~~~
23
php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-advanced /path/to/yii-application
24 25 26 27 28 29 30 31 32
~~~

Getting started
---------------

After you install the application, you have to conduct the following steps to initialize
the installed application. You only need to do these once for all.

1. Execute the `init` command and select `dev` as environment.
33 34 35
   ```
   php /path/to/yii-application/init
   ```
36
2. Create a new database and adjust the `components.db` configuration in `common/config/main-local.php` accordingly.
37 38
3. Apply migrations with console command `yii migrate`.
4. Set document roots of your Web server:
39

lancecoder committed
40 41
- for frontend `/path/to/yii-application/frontend/web/` and using the URL `http://frontend/`
- for backend `/path/to/yii-application/backend/web/` and using the URL `http://backend/`
Alexander Makarov committed
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
Directory structure
-------------------

The root directory contains the following subdirectories:

- `backend` - backend web application.
- `common` - files common to all applications.
- `console` - console application.
- `environments` - environment configs.
- `frontend` - frontend web application.

Root directory contains a set of files.

- `.gitignore` contains a list of directories ignored by git version system. If you need something never get to your source
  code repository, add it there.
- `composer.json` - Composer config described in detail below.
- `init` - initialization script described in "Composer config described in detail below".
- `init.bat` - same for Windows.
- `LICENSE.md` - license info. Put your project license there. Especially when opensourcing.
- `README.md` - basic info about installing template. Consider replacing it with information about your project and its
  installation.
- `requirements.php` - Yii requirements checker.
- `yii` - console application bootstrap.
- `yii.bat` - same for Windows.

68 69 70 71 72 73 74 75 76 77 78 79 80 81
Predefined path aliases
-----------------------

- @yii - framework directory.
- @app - base path of currently running application.
- @common - common directory.
- @frontend - frontend web application directory.
- @backend - backend web application directory.
- @console - console directory.
- @runtime - runtime directory of currently running web application.
- @vendor - Composer vendor directory.
- @web - base URL of currently running web application.
- @webroot - web root directory of currently running web application.

82 83 84 85 86 87 88 89 90 91 92 93
Applications
------------

There are three applications in advanced template: frontend, backend and console. Frontend is typically what is presented
to end user, the project itself. Backend is admin panel, analytics and such functionality. Console is typically used for
cron jobs and low-level server management. Also it's used during application deployment and handles migrations and assets.

There's also a `common` directory that contains files used by more than one application. For example, `User` model.

frontend and backend are both web applications and both contain `web` directory. That's the webroot you should point your
webserver to.

94 95
Each application has its own namespace and alias corresponding to its name. Same applies to common directory.

96 97 98
Configuration and environments
------------------------------

99 100
There are multiple problems with straightforward approach to configuration:

101
- Each team member has its own configuration options. Committing such config will affect other team members.
102 103 104 105 106
- Production database password and API keys should not end up in repository.
- There are multiple servers: development, testing, production. Each should have its own configuration.
- Defining all configuration options for each case is very repetitive and takes too much time to maintain.

In order to solve these issues Yii introduces environments concept that is very simple. Each environment is represented
107
by a set of files under `environments` directory. `init` command is used to switch between these. What it really does is
108 109
just copying everything from environment directory over the root directory where all applications are.

Luciano Baraglia committed
110
Typically environment contains application bootstrap files such as `index.php` and config files suffixed with
111 112
`-local.php`. These are added to `.gitignore` and never added to source code repository.

Carsten Brandt committed
113
In order to avoid duplication configurations are overriding each other. For example, frontend reads configuration in the
114 115
following order:

116 117
- `common/config/main.php`
- `common/config/main-local.php`
118 119 120 121 122 123 124 125 126 127 128
- `frontend/config/main.php`
- `frontend/config/main-local.php`

Parameters are read in the following order:

- `common/config/params.php`
- `common/config/params-local.php`
- `frontend/config/params.php`
- `frontend/config/params-local.php`

The later config file overrides the former.
129

130 131 132
Here's the full scheme:

![Advanced application configs](images/advanced-app-configs.png)
133 134 135 136

Configuring Composer
--------------------

Luciano Baraglia committed
137
After application template is installed it's a good idea to adjust default `composer.json` that can be found in the root
138 139
directory:

140
```json
141
{
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    "name": "yiisoft/yii2-app-advanced",
    "description": "Yii 2 Advanced Application Template",
    "keywords": ["yii", "framework", "advanced", "application template"],
    "homepage": "http://www.yiiframework.com/",
    "type": "project",
    "license": "BSD-3-Clause",
    "support": {
        "issues": "https://github.com/yiisoft/yii2/issues?state=open",
        "forum": "http://www.yiiframework.com/forum/",
        "wiki": "http://www.yiiframework.com/wiki/",
        "irc": "irc://irc.freenode.net/yii",
        "source": "https://github.com/yiisoft/yii2"
    },
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.4.0",
        "yiisoft/yii2": "*",
        "yiisoft/yii2-swiftmailer": "*",
        "yiisoft/yii2-bootstrap": "*",
        "yiisoft/yii2-debug": "*",
        "yiisoft/yii2-gii": "*"
    },
    "scripts": {
        "post-create-project-cmd": [
            "yii\\composer\\Installer::setPermission"
        ]
    },
    "extra": {
        "writable": [
            "backend/runtime",
            "backend/web/assets",

            "console/runtime",
            "console/migrations",

            "frontend/runtime",
            "frontend/web/assets"
        ]
    }
181 182 183 184 185 186 187
}
```

First we're updating basic information. Change `name`, `description`, `keywords`, `homepage` and `support` to match
your project.

Now the interesting part. You can add more packages your application needs to `require` section.
188
All these packages are coming from [packagist.org](https://packagist.org/) so feel free to browse the website for useful code.
189

190
After your `composer.json` is changed you can run `php composer.phar update --prefer-dist`, wait till packages are downloaded and
191
installed and then just use them. Autoloading of classes will be handled automatically.
Alexander Makarov committed
192 193 194 195 196

Creating links from backend to frontend
---------------------------------------

Often it's required to create links from backend application to frontend application. Since frontend application may
Castea Min committed
197
contain its own URL manager rules you need to duplicate that for backend application by naming it differently:
Alexander Makarov committed
198 199 200

```php
return [
201 202 203 204 205 206 207 208 209
    'components' => [
        'urlManager' => [
            // here is your normal backend url manager config
        ],
        'urlManagerFrontend' => [
            // here is your frontend URL manager config
        ],

    ],
Alexander Makarov committed
210 211 212
];
```

Castea Min committed
213
After it is done, you can get URL pointing to frontend like the following:
Alexander Makarov committed
214 215 216 217

```php
echo Yii::$app->urlManagerFrontend->createUrl(...);
```