We are going to deploy the new yiiframework.com website on March 23, 2018 from 8:00 to 12:00 UTC.
Website and Yii documentation will not be available in this time frame. Check the news for more details.

Yii 2.0: yii2-app-template

Yii 2 Application Template with ConfigKit

This is the proposed application boilerplate template by 2amigOS! for your Yii 2 applications.

The application comes with the configuration tooling named ConfigKit that we use to fire up our applications. With the kit you will be able to create your very own project structure template boilerplate, your very own customized way of using Yii.

The structure is pretty straight-forward and its files provide clear documentation of what is happening on each step. For example, this is the web app bootstrap file:

use SideKit\Config\ConfigKit;
use yii\web\Application;
 * --------------------------------------------------------------------------
 * Create Yii web application
 * --------------------------------------------------------------------------
 * Grab the configuration for the 'web' application and initialize an
 * Application instance with it. Remember that the file 'sidekit.php' has to
 * be *required* prior inserting this script so we can access the environment
 * variables.
$config = ConfigKit::config()->build('web', ConfigKit::env()->get('CONFIG_USE_CACHE'));
$app = new Application($config);
 * --------------------------------------------------------------------------
 * Return application
 * --------------------------------------------------------------------------
 * Return instance to calling script, that way we separate initialization
 * processes and liberate the entry script with too much code.
return $app;

Here is an brief explanation of the structure:

app                 [ Yii's application related code: commands, components, controllers, bundles, models, modules, 
|                     views and widgets ]
├── assets          [ contains asset's definitions ]
├── commands        [ contains Console commands (Yii names them controllers) ]
├── controllers     [ contains Web controller classes ]
├── models          [ contains model calsses ]
└── views           [ contains view files for the Web application ]
bin                 [ contains command-line executable scripts ]
bootstrap           [ contains bootstrap process files ]
config              [ contains application configuration files ]
public              [ contains Web application entry script + static resources ]
runtime             [ contains files generated during application's runtime ]
src                 [ contains domain business logic files. Portable code, free of Yii's code. Build your library here. ]
tests               [ contains codeception tests for your application ]

As you can see, the structure includes a src folder and you may wonder, why is that folder there if we follow the MVC pattern? Why does it says that we should include domain business logic there? And you probably thinking the same with that gulpfile.js, if Yii handles all that, why that file exists?

You can do whatever you want, in fact, every developer has its own way to do things and Yii is a great framework with an amazing flexibility to please every taste that is not a php-purist. But, if you are a developer who likes to listen from other people's experiences and you think your application is going to escalate to another dimension, please take this important advice:

  • DO NOT include your business logic in your models and/or controllers. Keep your models thin and your controllers even thinner.

Create your business logic following D.R.Y. and S.O.L.I.D. whenever possible on that src folder. Add your custom namespaces to composer.json file and use the amazing Yii's container with composer autoloader super-powers, is awesome, really.

If you application grows, you will be happy to see how portable your code is; that you don't need to dig in your models, controllers and even views (yes, I have seen that too with widgets). Failing to do that, you will end up in a bottleneck where all possible changes to the core of your app will be highly expensive.

Further Information & download

If you wish to find out more about this template and ConfigKit, please visit the following github links:

Any feedback is always welcome, as always.


Total 4 comments

#20090 report it
Antonio Ramirez at 2017/06/13 11:04am

Yes, Andrezj, you are correct

#20037 report it
PeRoChAk at 2017/02/27 09:25pm
attaching modules


you can attach gii module as config/web/modules/gii.php and define it as

return [
    'class' => 'yii\gii\Module',
#20035 report it
Andrzej at 2017/02/26 05:58am
gii, debug

How to turn on gii and debug extension?

#20034 report it
Andrzej at 2017/02/26 05:46am
new modules

Hi, For new modules just add a config file named like the module in config\web\modules dir?

Thanks, AO

Leave a comment

Please to leave your comment.

Create extension
No downloadable files yet