Complete Yii2 Advanced Setup Tutorial

Advertisement

Building modern web applications with separate frontend and admin panel architectures requires a solid foundation. The Yii2 Advanced Template is a structured solution offering just that, complete with a comprehensive user management system. This comprehensive guide will walk you through the installation, configuration, and optimization of your development environment for this template.

Advertisement

In medium to large-scale web application development, choosing the right framework is crucial. The Yii2 Advanced Template emerges as a more robust solution compared to the Basic version. This template is specifically designed for projects requiring clear separation of business logic between the frontend (end-user application) and the backend (administrator dashboard).

The primary reason developers switch to the Yii2 Advanced Template is the availability of a comprehensive and ready-to-use user management system. This includes registration, login, logout, email verification, and password reset functionalities, all implemented with good security standards. Moreover, its modular architecture allows for further expansion, such as adding separate applications for moderator, manager roles, or isolated API services.

Advertisement
AspectYii2 Basic TemplateYii2 Advanced Template
Application StructureSingle ApplicationMulti-Application (Frontend, Backend, Console)
User ManagementBasic Login/Logout OnlyComplete System (Signup, Reset Password, etc.)
Base Code (Bootstrap)AvailableAvailable
SuitabilitySimple Apps, MVPComplex, Enterprise Apps
Complexity LevelLow to MediumMedium to High
In-Depth Comparison: Yii2 Basic vs. Advanced Template. Your template choice greatly determines project scalability and code structure.

Mandatory Preparations Before Installation

Before starting the Yii2 Advanced Template installation, ensure your development environment meets all technical requirements. Thorough preparation prevents errors and saves time.

Advertisement
  • Web Server: You need a local server like XAMPP (Windows), Laragon (Windows), MAMP (Mac), or LAMP (Linux). Ensure PHP version 7.4.0 or higher is installed. PHP 8.0+ is highly recommended for the latest performance and security. Download the latest XAMPP from the official Apache Friends site.
  • Composer: This is a PHP dependency manager that is mandatory. Ensure Composer is installed globally on your system. Verify by running the composer --version command in Command Prompt or Terminal. If not, install it from GetComposer.org.
  • PHP Extensions: Ensure the following extensions are active in your php.ini file: pdo, pdo_mysql (or pdo_pgsql for PostgreSQL), mbstring, openssl, and intl. These are usually active by default in XAMPP packages.
  • Database: Prepare a database server (MySQL/MariaDB recommended) and create one empty database to be used by the Yii2 application.

Step 1: Install Yii2 Advanced via Composer

The installation process using Composer is the most recommended method as it will manage all library dependencies automatically and in a structured manner.

  1. Open Command Prompt (Windows) or Terminal (Mac/Linux).
  2. Navigate (cd) to your local server’s htdocs directory (e.g., cd C:\xampp\htdocs or cd /Applications/MAMP/htdocs).

3. Run the following installation command. This command will create a new folder named yii2-advanced containing the entire template code.

composer create-project --prefer-dist yiisoft/yii2-app-advanced yii2-advanced
  1. Wait for the process to complete. Composer will download all required packages, including the Yii2 framework itself, SwiftMailer, and other supporting libraries. This process requires a stable internet connection and may take a few minutes.

Important Note for Composer 2.x: Unlike older tutorials, you no longer need to run the command composer global require "fxp/composer-asset-plugin:~1.1.1". That plugin is no longer supported and has been replaced by native methods in Composer version 2. So, you can directly run the create-project command.

Step 2: Initialization and Basic Configuration

Once the template files are successfully downloaded, the next step is to initialize the environment and configure the application.

  1. Run the Init Script: Enter the project directory (cd yii2-advanced). Inside, run the init.bat file (Windows) or init (Linux/Mac) in the terminal. Select the [0] Development option for development mode. If prompted for confirmation, type yes.
  2. Configure Database: Open the common/config/main-local.php file with your favorite text editor. Find the components['db'] section and adjust your database connection details.
'db' => [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=your_database_name',
    'username' => 'root', // your database username
    'password' => '', // your database password
    'charset' => 'utf8',
],
  1. Run Database Migration: In the terminal, still within the project directory, run the following command to automatically create the table schemas (including the user table).
php yii migrate

Press Yes when prompted to confirm running the migration. This process will create all necessary tables, including those for user management, logs, and others.

To access the frontend and backend with clean, separate URLs (e.g., yii2-frontend.test and yii2-backend.test), configuring a virtual host in Apache is highly recommended. This replicates a production environment and eases development.

  1. Edit httpd-vhosts.conf: Open the C:\xampp\apache\conf\extra\httpd-vhosts.conf file (adjust the path if your XAMPP is located elsewhere). Add the following configuration at the end of the file:
<VirtualHost *:80>
    ServerName yii2-frontend.test
    DocumentRoot "C:/xampp/htdocs/yii2-advanced/frontend/web"
    <Directory "C:/xampp/htdocs/yii2-advanced/frontend/web">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        # Yii2 routing configuration (URL Rewrite)
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName yii2-backend.test
    DocumentRoot "C:/xampp/htdocs/yii2-advanced/backend/web"
    <Directory "C:/xampp/htdocs/yii2-advanced/backend/web">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
        # Same routing configuration for backend
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
    </Directory>
</VirtualHost>
  1. Edit Hosts File: Open the C:\Windows\System32\drivers\etc\hosts file as Administrator (use Notepad “Run as administrator”). Add these two lines at the end of the file:
127.0.0.1   yii2-frontend.test
127.0.0.1   yii2-backend.test
  1. Restart Apache via the XAMPP control panel.

Access and Test the Yii2 Application

After completing all the above steps, you are ready to access and test your Yii2 Advanced Template installation.

To test the user management features, click the “Signup” link on the frontend page. Fill out the registration form with an email, username, and password. After successful registration, you can log in immediately. Also try the “Reset Password” feature to ensure its workflow functions correctly.

Conclusion and Next Steps

By following this guide, you have successfully installed and configured the Yii2 Advanced Template in your local development environment. This template provides a very strong foundation for building complex, scalable, and secure web applications.

The logical next steps after installation are to learn Yii2’s built-in Role-Based Access Control (RBAC) for more detailed user permission management in the backend, configure email sending for verification features, and understand the common, console, frontend, and backend directory structures for optimal feature development.

Latest Articles