Home >> Latest Articles >> How to install Yii framework using composer

How to install Yii framework using composer

How to install Yii framework using composer: step by step guide for Yii framework installation using composer on different operating systems.
Currently, there are two Yii framework templates available:
  1. Basic Template – a basic frontend application template.
  2. Advanced Template – consisting of a frontend, a backend, console resources, common (shared code), and support for environments.

There is no difference as the underlying core framework code is the same. The difference is only the structure of the project - the most obvious difference is that advanced one already has a "backend" set-up for you. So, always use Advanced template with Lateste stable release.

Note: Replace "ProjectName" with your actual project folder name

You may also like: Core PHP Interview Questions Answers
Methods of Yii2 Installation:
1. Install from an Archive File
  1. Download Yii framework from Github or Click to direct download Yii 2 .
  2. After downloading and extracting the archive files, copy and paste it to your web-accessible folder. In my case it's in C:/xampp/htdocs/ProjectName.
  3. Now type the following code in command prompt (cmd) to initialize the init file C:/xampp/htdocs/ProjectName init OR C:/xampp/htdocs/ProjectName php init
  4. Now let’s create a fresh new database. let’s configure database details. C:/xampp/htdocs/ProjectName/common/config/main-local.php

    Note: if main-local.php file is not there create it by yourself and put the following code in it

    Code:
     return [ 
       'components' => [
            'db' => [
                'class' => 'yii\db\Connection',
                'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
                'username' => 'root',
                'password' => '',
                'charset' => 'utf8',
            ],
            'mailer' => [
                'class' => 'yii\swiftmailer\Mailer',
                'viewPath' => '@common/mail',
                // send all mails to a file by default. You have to set
                // 'useFileTransport' to false and configure a transport
                // for the mailer to send real emails.
                'useFileTransport' => true,
            ],
        ],
    ];
    
    
  5. Next is database migration, lets apply the migration with console command c:/xampp/htdocs/ProjectName yii migrate OR c:/xampp/htdocs/ProjectName php yii migrate

All set, now it is time to check your frontend & backend:


Frontend Files: C:\xampp\htdocs\ProjectName\frontend\web

URL: http://localhost/ProjectName/frontend/web/


Backend Files: C:\xampp\htdocs\ProjectName\backend\web

URL: http://localhost/ProjectName/backend/web/


You may also like: OOPS Interview Questions and Answers
2. Install Yii framework via Composer
  1. Note- if you don't have composer installed in your system you can install it from here click to install composer
  2. check composer version using this command : composer -v
  3. To install "Yii2 Advanced Application Template", use the following code
     composer create-project --prefer-dist --stability=dev yiisoft/yii2-app-advanced ProjectName
    

Yii framework will have the following directory structure.

  • backend - backend web application files & folders.
  • common - files common to all applications.
  • console - console application files & folders.
  • environments - environment configs.
  • frontend - frontend web application files & folders.

All set, now it is time to check your frontend & backend:


Frontend Files: C:\xampp\htdocs\ProjectName\frontend\web

URL: http://localhost/ProjectName/frontend/web/


Backend Files: C:\xampp\htdocs\ProjectName\backend\web

URL: http://localhost/ProjectName/backend/web/


For more info please follow - Yii2 Official website Link

Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles