Yii2 provides two application templates: Basic and Advanced. The Basic Template is suitable for small to medium projects, while the Advanced Template is designed for large-scale applications with multiple environments.
In this tutorial, we will cover:
Before installing Yii2, ensure your system meets the following requirements:
To check if PHP and Composer are installed, run:
php -v
composer -VIf Composer is not installed, download it from getcomposer.org.
The Basic Template is the recommended starting point for most projects.
Run the following command to create a Yii2 Basic project:
composer create-project --prefer-dist yiisoft/yii2-app-basic dynamic-duniya-basicThis will install Yii2 Basic in a folder named dynamic-duniya-basic.
cd dynamic-duniya-basicphp yii serveNow, open your browser and visit:
http://localhost:8080You should see the Yii2 welcome page.
In the Basic Template, configure the database in:
// config/db.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2basic',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];The Advanced Template is designed for enterprise-level applications and provides separate environments for frontend, backend, and console applications.
composer create-project --prefer-dist yiisoft/yii2-app-advanced dynamic-duniya-advancedYii2 Advanced requires an initialization step:
cd dynamic-duniya-advanced
php initYou will be asked to choose an environment:
Which environment do you want the application to be initialized in?
[0] Development
[1] ProductionSelect 0 for development or 1 for production.
Edit the database configuration file:
// common/config/main-local.php
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
],
];Now, run the migration command to set up the database schema:
php yii migrateRun the PHP server for both frontend and backend:
php yii serve --docroot="frontend/web"
php yii serve --docroot="backend/web"Visit http://localhost:8080 for the frontend and http://localhost:8080/admin for the backend.
dynamic-duniya-basic/
│── assets/ # Compiled CSS & JS files
│── commands/ # Custom console commands
│── config/ # Configuration files
│── controllers/ # Application controllers
│── models/ # Business logic and database models
│── runtime/ # Temporary files (cache, logs)
│── views/ # Application views (HTML)
│── web/ # Web-accessible root directorydynamic-duniya-advanced/
│── backend/ # Backend application
│── frontend/ # Frontend application
│── common/ # Shared code between frontend & backend
│── console/ # Console commands & scripts
│── environments/ # Config for different environments
│── vendor/ # Installed dependenciesThe Advanced Template is ideal when you need separate frontend and backend applications.
For the Basic Template:
php yii serveFor the Advanced Template (Frontend)
php yii serve --docroot="frontend/web"For the Advanced Template (Backend):
php yii serve --docroot="backend/web"You Can Start Development But i Recommend you to Create Virtual host for Basic or Advanced template whatever you choose.
If using Apache or Nginx, point the document root to the web/ directory.
Sign in to join the discussion and post comments.
Sign in