Home | About | View All Posts

15 May 2022

How to install Yii2 using composer method

The Yii 2.0 is the latest version of the Yii framework. The Yii is one of the most popular, efficient and robust free open source PHP framework which is used for developing the modern and commercial web applications.

You need to install the Yii framework at the server for creating the web applications. It requires a running instance of web server with PHP 5.4.0 or above. We have used XAMPP(Apache web server, MySQL, PHP) server for setting up the Yii2 application in Windows 10 operating system.

The Yii2 standard installation provide both the framework files and a project template - "Basic Project Template(without backend and frontend folder structure)" and "Advanced Project Template(with backend and frontend folder structure)". The Yii2 provides pre coded or pre-programmed features for home page, register, login, contact form, about page in the project template. You can install one of the two project template as per your project or application requirement.

How to install Yii2? -
We can install the Yii using two ways, which are divided and explained as below -

How to install Yii2 using composer method
  1. Using Composer package manager
  2. By downloading the archive file

The following are details for each step that you can follow for "How to install Yii2" or "How to install Yii2 using composer " or "How to install Yii2 in windows" -

  1. Using Composer package manager -
    Following are steps for YII2 advanced installation -
    1. Installing composer
    2. Getting yii2 app files from yii git repository
    3. Yii application environment initialization
    4. Database creation
    5. Running and verifying the yii2 installation
    6. Invoking sign up functionality


    1. Installing composer in Windows machine -
      You can check your windows machine – if it has already composer installed. You need to press the combination of Windows and “R” key for opening the “Run” dialog box. Now write “cmd” in the open box for opening the command window. You need to write the command : “composer –v”. The composer version screen will be shown as following screenshot if you have already composer installed in your windows machine.



      If you have not pre installed composer at your machine then you can install the composer using the windows installer or by using command-line installation. The first way - "Windows installer" is the easiest way to get Composer set up on your machine.

      You need to download and run "composer-setup.exe" from "https://getcomposer.org". It will install the latest composer version and set up your PATH environment variable so that you can simply call 'composer' from any directory.

      Now for verifying the composer installation, please press the combination of Windows and “R” key for opening the “Run” dialog box. Now write “cmd” in the open box for opening the command window. You need to write the command : “composer –v” and press the enter key. It will show composer version screen as the above command window screenshot.

    2. Getting yii2 app files from yii git repository
      Navigate and go at the preferred directory, where you want to install yii application. The following path shows the directory path in my case at XAMPP server -

      C:\xampp\htdocs>

      At here we will use the 'create-project' command for installing Yii2 framework application as following -

      For Basic application(without backend and frontend folder structure) -
      C:\xampp\htdocs>composer create-project --prefer-dist yiisoft/yii2-app-basic yii2cmstest

      For Advanced application(with backend and frontend folder structure) -
      C:\xampp\htdocs>composer create-project --prefer-dist yiisoft/yii2-app-advanced yii2cmstest

      Here,
      Command option : --prefer-dist
      Package : yiisoft/yii2-app-basic
      Directory : yii2cmstest

      Generally package url relates with github which is - https://github.com/yiisoft/yii2-app-basic. Directory will be automatically created that you have provided as name at above command line eg. 'yii2cmstest'.

      Please note - application will be installed in an auto created folder "yii2-app-basic or yii2-app-advanced", if you do not provide folder name. Also you need to answer the prompted questions in the command window during the installation process.

      You can use --dev or --no dev command option if you want to install production or development application.

      Finally all dependent module or packages like - bower-asset/bootstrap, yiisoft/yii2-swiftmailer, yiisoft/yii2-gii, symfony/yaml etc will be installed. Following is the screenshot for installation process.



    3. Yii application environment initialization
      Now run the “init” file at command window for running the Yii application initialization tool -

      C:\xampp\htdocs\yii2cmstest>init

      You can also click and run the “init.bat” in the folder path –“ C:\xampp\htdocs\yii2cmstest” , instead of above command for initializing the Yii application.

      The yii “init” file creates necessary files at appropriate directory for Yii application environment initialization as the following screenshot -



    4. Database creation
      Now open the configuration file “main-local.php” that have created by “init” command at C:\xampp\htdocs\yii2cmstest\common\config\ folder.

      In my case configuration file path is -
      C:\xampp\htdocs\yii2cmstest\common\config\main-local.php

      We will adjust the components['db'] configuration here. The Yii installation system write and use database named as “yii2advanced” by default in the database connection string in the components['db'] configuration.

      We will create a database named as same “yii2advanced” in our phpmyadmin so that signup and login functionality work properly. The user entry is taken place in this database while signup process.

      Please note - You are free to change the name of database if you do not want to use database name that has been created by default while Yii installation.



    5. Running and verifying the yii2 installation
      We will verify and run the Yii installation process. You need to go and browse the url below to check the installation process of backend - “ /advanced/backend/web/” or frontend - “/advanced/frontend/web/”.

      In my case it was -
      http://localhost/yii2cmstest/frontend/web/
      http://localhost/yii2cmstest/backend/web/

      The following congratulation page(screenshot) will be shown after successful installation at -
      http://localhost/yii2cmstest/frontend/web/



      The login form(screenshot) would visible after successful installation if you browse the backend at -
      http://localhost/yii2cmstest/backend/web/



    6. Invoking sign up functionality
      The Yii installation system provide a pre built “Sign up” form in the frontend. You can view and check this page by clicking at the “signup” link in the top navigation bar. You can view this page directly using the following url -

      http://localhost/yii2cmstest/frontend/web/index.php?r=site%2Fsignup

      The above sign up form will not place user entry into the database. We need to invoke its functionality before start of the signup process.

      We need to run the migrate command at command window for invoking sign up functionality as following. This migration command primarily sets up the table for user management.

      Please go at command prompt and run the command -

      C:\xampp\htdocs\yii2cmstest >yii migrate

      It will run as below -
      Yii Migration Tool (based on Yii v2.0.6)
      
      Creating migration history table "migration"...Done.
      Total 1 new migration to be applied:
          m130524_201442_init
      
      Apply the above migration? (yes|no) [no]:yes
      *** applying m130524_201442_init
          > create table {{%user}} ... done (time: 0.007s)
      *** applied m130524_201442_init (time: 0.022s)
      
      
      Migrated up successfully.
      

      The two new table - "migration" and "user" is created in the phpmyadmin due to this migration command. The following is the screenshot of the phpmyadmin with two newly created table name.



      Finally go at sign up form and do sign up using mail and password then try to login into your Yii2 project.

      In my case sign up url was -
      http://localhost/yii2cmstest/frontend/web/index.php?r=site/signup

      In my case login url was -
      http://localhost/yii2cmstest/frontend/web/index.php?r=site/login

      Please visit the page at “Yii2 advanced login not working after installation” if you face problem in the login process after Yii2 installation.
  2. By downloading the archive file -
    Please browse the Yii official download page at http://www.yiiframework.com/download/ for viewing the download archive button for – “basic application template” and “advanced application template” – as following screenshot. You can download the archive file for basic or advanced template as per your requirement.



    Please extract the archive file downloaded from the above url at your custom project folder root at the server or at the root of the server as per your needs.

    Now follow the instructions that has been mentioned in the step 3 to step 6 above.
If you are preparing and looking for a career in Yii, please visit at "Interviews Questions and Answers" for getting your dream job.

Tags :
Aashutosh Kumar Yadav

By Aashutosh Kumar Yadav

He is a PHP-based UI/Web designer and developer by profession and very interested in technical writing and blogging. He has been writing technical content for about 10 years and has proficient in practical knowledge and technical writing.
@www.infotokri.in

0 comments:

Post a Comment