Advanced Template Directory Structure

  • backend
  • As mentioned, with the advanced template, you actually have two Yii installs. The backend directory is a complete Yii structure, similar to what you will
    find in the Basic Template. You will find the following subdirectories:

    • assets
    • config
    • controllers
    • models
    • runtime
    • views
    • web

    Each of these directories offers the same functionality as the corresponding directories as explained in the Basic Template above.

    One item to make note of here though is the config directory. Any configurations included in this config directory will apply only to web requests invoking
    the backend.

  • common
  • The purpose of the common directory is to have it accessed by both backend and frontend. You will see the following subdirectories:

    • config
    • Config files in this directory will apply to both backend and frontend.

    • mail
    • Functions same as the mail found in the Basic Template described above.

    • models
    • If you are using both backend and frontend, this is an important directory. The models directory in common allows you to access the same database functions
      from backend and frontend. If you are pulling the same data in particular instances, it makes sense to have just one model set up for that.

    • widgets
    • The widgets directory is a handy place to put snippets of code that you want to access anywhere, frontend or backend. By default, there is one widget
      already included that you will surely make use of: Alert.php

      A nice feature of Yii is the flash messaging. You can set a message that will be displayed on the next page loaded. You may have an alert that says there
      was an error submitting some info or a message saying info was successfully saved. These messages can be displayed simply using the following:

      <?php Alert::begin(); ?>

  • console
  • The console directory provides similar functionality to the command directory in the Basic Template, with some added features.

    As with the command directory, you can execute scripts from the command line. The scripts that will be executed are put in the controllers subdirectory.
    Here is an example of the same console file that will output “hello world” when executing the following on command line: php yii hello

    <?php
    
    namespace console\controllers;
    
    use yii\console\Controller;
    
    class HelloController extends Controller
    {
        public function actionIndex($message = 'hello world')
        {
            echo $message . "\n";
        }
    }
    

    In addition to the controller subdirectory, you have the following:

    • config
    • Set up specific config settings that apply only to using the console in command line terminal

    • migrations
    • Migrations is a powerful feature of Yii that enhances it portability. You can put full database schemas into the migrations folder and it will create the
      database tables for you as needed by executing the following command:


      php yii migrate

      You can even set the migrations up so that they can easily be reverted if need be.

    • models
    • Database functions specifically for command line functions.

    • runtime
    • This directory is for Yii internal use and can be ignored.

  • environments
  • The environments directory has two subdirectories:

    • dev
    • prod

    You can think of the environments as templates that can be applied by executing a command in terminal environment.

    Both these subdirectories have the following additional subdirectories:

    • backend
    • common
    • console
    • frontend

    If you set up Yii using the advanced template above, you made use of the environment function when you typed the following in command line terminal:


    php init

    and selected Development or Production.

    When you run the init command and select one of the above, it overwrites the various corresponding files with the template files for dev or prod.

  • frontend
  • The directory structure and functionality of the frontend directory is identical to the backend.

  • tests
  • The tests folder is set up to facilitate functional testing using Codeception.

  • vendor
  • Again, vendor is where all the source files for Yii reside. Refer to the Basic Template explanation above for details.

Published by

Joel Bowers

Web developer since 1999. PHP, YII2, Laravel, Javascript, HTML, CSS, jQuery, Perl, Wordpress, MySQL.

Leave a Reply

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