PHP Web Framework Yii2

Default Page For Installed Yii2 Framework

Live stream set for 2025-06-22 at 14:00:00 Eastern

Ask questions in the live chat about any programming or lifestyle topic.

This livestream will be on YouTube or you can watch below.


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.

About Edward

Edward is a software engineer, web developer, and author dedicated to helping people achieve their personal and professional goals through actionable advice and real-world tools.

As the author of impactful books including Learning JavaScript, Learning Python, Learning PHP and Mastering Blender Python API, Edward writes with a focus on personal growth, entrepreneurship, and practical success strategies. His work is designed to guide, motivate, and empower.

In addition to writing, Edward offers professional "full-stack development," "database design," "1-on-1 tutoring," "consulting sessions,", tailored to help you take the next step. Whether you are launching a business, developing a brand, or leveling up your mindset, Edward will be there to support you.

Edward also offers online courses designed to deepen your learning and accelerate your progress. Explore the programming on languages like JavaScript, Python and PHP to find the perfect fit for your journey.

📖 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.
🎓 Ready to Learn? – Check out his Online Courses to turn your ideas into results.

Leave a Reply

Your email address will not be published. Required fields are marked *