PHP Web Framework Yii2

Default Page For Installed Yii2 Framework
Revised 3 min, 45 sec read

Getting Started with Yii2: How to Install and Build a Sample App that Uses a Database

If you’re a developer looking to build fast, secure, and feature-rich PHP applications, Yii2 is an excellent choice. Yii2 is an open-source, high-performance PHP framework that makes developing modern web applications easy and enjoyable.

In this article, we’ll walk you through the steps to:

  • Install Yii2
  • Set up a basic app
  • Connect to a database
  • Pull data from a sample table
  • List the supported databases

What is Yii2?

Yii2 stands for “Yes, it is!” — and for good reason. It’s a full-stack framework designed for building robust and scalable web applications. It supports rapid development, secure coding practices, and is optimized for performance.

Yii2 follows the MVC (Model-View-Controller) Architecture that makes code more organized, maintainable, and testable..

Open Source

Yii is released under the BSD-3-Clause Revised License. The permissive license requires the preservation of the copyright notice and disclaimer. It allows commercial use, modification, distribution, and allows making derivatives proprietary, consult the license for more specific details.

The PHP scripting language is licensed under the PHP License. The permissive license has conditions requiring preservation of copyright and license notices. Redistribution is permitted in source or binary form with or without modifications, consult the license for more specific details.

Requirements For Yii2

Glossary

Model

Represents the data and business logic. Interacts with the database (CRUD operations), performs calculations, and handles data processing.

View

Responsible for displaying the user interface. Contains HTML, CSS, and potentially some PHP for data presentation.

Controller

Acts as the intermediary. Receives user requests, loads models, interacts with them to retrieve data, and then loads the appropriate views to display the results.

How to Install Yii2

To install Yii2, you’ll need Composer. If you haven’t installed it yet, download Composer here.

Step 1: Create a New Project

composer create-project --prefer-dist yiisoft/yii2-app-basic myapp

Step 2: Set File Permissions

chmod -R 777 myapp/runtime
chmod -R 777 myapp/web/assets

Step 3: Run the Built-In PHP Server

cd myapp
php yii serve

Then go to http://localhost:8080 in your browser.

Setting Up the Database Connection

Yii2 uses a database configuration file at config/db.php.

   return [
      'class' => 'yii\db\Connection',
      'dsn' => 'mysql:host=localhost;dbname=mydatabase',
      'username' => 'root',
      'password' => '',
      'charset' => 'utf8',
   ];
  

Creating a Sample Table and Pulling Data

Create Table SQL

   CREATE TABLE post (
      id INT AUTO_INCREMENT PRIMARY KEY,
      title VARCHAR(255) NOT NULL,
      content TEXT NOT NULL
   );
  

Insert Sample Data

   INSERT INTO post (title, content) VALUES
   ('First Post', 'This is the first post.'),
   ('Second Post', 'This is the second post.');
  

Generate a Model with Gii

Visit Gii code generator:

http://localhost:8080/index.php?r=gii

  • Use the Model Generator
  • Enter post as table name
  • Click Preview, then Generate

Display Data from the Model

   use app\models\Post;

   public function actionPosts()
   {
       $posts = Post::find()->all();
       return $this->render('posts', ['posts' => $posts]);
   }
   

Create a view at views/site/posts.php:

   <?php foreach ($posts as $post): ?>;
   <h2<?= $post->title ?></h2>
   <p><?= $post->content ?></p>
   <?php endforeach; ?>
  

Supported Databases in Yii2

  • MySQL / MariaDB
  • PostgreSQL
  • SQLite
  • Microsoft SQL Server
  • Oracle (via extensions)

Screenshots

Yii2 Installation In Terminal
Command Line Yii2 Installation Using Composer

Yii2 Permissions In Terminal
Command Line Yii2 Permissions Using Composer

Yii2 Server In Terminal
Command Line Yii2 Web Server Running

Yii2 Database Configuration
Netbeans IDE Displaying Yii2 Database Configuration File

Yii2 Database Table
PHPMyAdmin Displaying Yii2 Database Table Creation SQL

Yii2 Database Table Records
PHPMyAdmin Displaying Yii2 Database Table Insertion SQL

Gii Model Generator Tool
Web Browser Displaying Gii Generator Screen

Yii2 Generated Model
Netbeans IDE Displaying Yii2 Generator Model File

Yii2 View
Netbeans IDE Displaying Yii2 View File

Yii2 Controller
Netbeans IDE Displaying Site Controller File

Screencast

Watch the full walkthrough on YouTube:

Yii2 Custom View Record In Web Browser

Need Help?

Need help setting this up? Feel free to leave a comment or contact me directly and I’ll be happy to help!

Summary

  • Installed Yii2 via Composer
  • Configured a database
  • Created a model using Gii
  • Displayed database content on a web page

Yii2 is a fast, secure, and powerful PHP framework that can get your apps running in no time.

🚀 Recommended Resources


Disclosure: Some of the links above are referral links. I may earn a commission if you make a purchase at no extra cost to you.

About Edward

Edward is a software engineer, author, and designer dedicated to providing the actionable blueprints and real-world tools needed to navigate a shifting economic landscape.

With a provocative focus on the evolution of technology—boldly declaring that “programming is dead”—Edward’s latest work, The Recession Business Blueprint, serves as a strategic guide for modern entrepreneurship. His bibliography also includes Mastering Blender Python API and The Algorithmic Serpent.

Beyond the page, Edward produces open-source tool review videos and provides practical resources for the “build it yourself” movement.

📚 Explore His Books – Visit the Book Shop to grab your copies today.

💼 Need Support? – Learn more about Services and the ways to benefit from his expertise.

🔨 Build it Yourself – Download Free Plans for Backyard Structures, Small Living, and Woodworking.