Changes
Title
unchanged
Organize directories for applications with front-end and back-end
Category
unchanged
Tutorials
Yii version
changed
1.1
Tags
unchanged
Content
changed
[...]
models/
views/
runtime/
~~~
We have two entry scripts here: `index.php` and `backend.php`. The former is used by front-end, while the latter by back-end. All the application code are placed under the [base application directory](/doc/guide/1.1/en/basics.application#application-base-directory) `protected` which should be configured to prevent from being accessed directly by end users.
Under `protected`, we have the normal set of sub-directories needed by a typical Yii application: `config`, `components`, `controllers`, `models`, `views` and `runtime`.[...]
The entry script code for the front-end and the back-end look like the following. Their main difference is that different application configurations are used.
```php
// index.php:
require('path/to/yii.php');
Yii::
app()->createWebApplication('protected/config/main.php')->run();
// backend.php:
require('path/to/yii.php');
Yii::
app()->createWebApplication('protected/backend/config/main.php')->run();
```
The front-end application configuration is very normal, just like we usually have for single-end applications. The back-end application configuration is a bit special. Its content is given as follows,
```php
$backend=dirname(dirname(__FILE__));
$frontend=dirname($backend);[...]
```
In the above, we first define `$backend` and `$frontend` to be the directory `protected/backend` and `protected/`, respectively. We then define a root alias named `backend` to be the directory `protected/backend`. In the configuration array, we specify that the [base application directory](/doc/guide/1.1/en/basics.application#application-base-directory) of the back-end to be the same as that of the front-end, namely, `protected/` (the reason of doing so is to explained shortly). The rest of the crucial paths (`controllerPath`, `viewPath` and `runtimePath`) are defined to be located under `protected/backend`. And finally, we import several directories, starting with the back-end `components` and `components` directories, followed by the normal application `components` and `components` directories.
So why are we using `protected` as the [base application directory](/doc/guide/
1.1/en/basics.application#application-base-directory) for both the front-end and the back-end? This is because the back-end often needs to reuse the code designed for the front-end, but not vice versa. Having the same [base application directory](/doc/guide/
1.1/en/basics.application#application-base-directory) means that the two ends have the same path for the `application` root path alias. Therefore, code referring to the `application` alias can be reused without any problem in both ends.
The back-end, in addition to reusing the front-end code, usually has its own special code to deal with, for example, content administration. We store these code under the `protected/backend/` directory and sub-directories. In its application configuration, we also import these additional sub-directories together with those meant for both of the ends.