my common yii2 dev environment

In light of a recent discussion I had with a developer regarding Yii2 application setup, I thought I’d share my common setup. While it really comes down to the individual project, here a breakdown of my “go to” setup.

app/           - contains project source 
web/           - contains entry script and public resources (js, css ..)
vendor/        - contains composer source files
node_modules/  - contains node modules used for grunt 
runtime/       - contains runtime files (debug, cache ..)
.gitignore     - contains a list of files to be ignored by git
composer.json  - composer package file
Gruntfile.js   - grunt file for js & css operations
package.json   - grunt package file
README.md      - project README file
yii            - yii cli file

It’s a biggest diffrence from the Yii 2.0 Basic Application Template,  is it’s complete seperation of app files and web.  The app folder contains

assets/
commands/
components/
config/
mail/
messages/
models/
modules/
tests/
views/
widgets/

The entry file (web/index.php) is also a bit different.

run();

The main purpose of this is to limit the amount of files that need to be modified. A user can create a local config file on their machine (which git ignores).

It always the user to edit constants and config parameters in one place.

Here is an example of a config.local.php file

 [
     'class' => 'yii\db\Connection',
     'dsn' => 'mysql:host=localhost;dbname=db',
     'username' => 'root',
     'password' => '',
     'charset' => 'utf8',
     'tablePrefix' => '',
     'enableSchemaCache' => true,
 ],
 'mailer' => [
     'class' => 'yii\swiftmailer\Mailer',
     'useFileTransport' => true,
     'transport' => [
         'class' => 'Swift_SmtpTransport',
         'host' => 'host',
         'username' => 'user',
         'password' => 'pass',
         'port' => '587',
     ],
 ],
 'request' => [
     'cookieValidationKey' => 'keygoeshere',
 ]
];

Pretty straight forward. Here is web.php which uses the $configExt variable

 'app',
    'basePath' => $basePath,
    'name' => 'App Name',
    'bootstrap' => ['log'],
    'language' => 'en',
    'timeZone' => 'Europe/Warsaw',
    'sourceLanguage' => 'en',
    'runtimePath' => $webroot . '/runtime',
    'vendorPath' => $webroot . '/vendor',
    'components' => [
        'request' => $configExt['request'],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => $configExt['mailer'],
        'urlManager' => require(__DIR__ . '/urlmanager.php'),
        'assetManager' => $configExt['assetManager'],
        'log' =>  $configExt['log'],
        'db' => $configExt['db'],
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ]
];

if (YII_DEBUG) {
    // configuration adjustments for 'dev' environment
     $config['bootstrap'][] = 'debug';
     $config['modules']['debug'] = 'yii\debug\Module';

    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = 'yii\gii\Module';

    $config['components']['db']['enableSchemaCache'] = false;
}

return $config;

On side a note, I almost always use the built in php web server for yii development. With this setup, using

php yii serve

will return a Document root “C:\testapplication\app/web” does not exist. error. So I have to use the –docroot option. Like this

php yii serve --docroot="web"

I mainly like this setup because of it’s simplicity. I rarely work on js & css files and this does a better job of seperating these files. Also seems to make the project more friendly to look at on github 🙂 Hope you find this setup helpful! Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *