Getting Started with Yii on Windows and MariaDB

Author

Reads 10K

Adult practicing Tai Chi surrounded by incense with Yin Yang symbol and smoky atmosphere.
Credit: pexels.com, Adult practicing Tai Chi surrounded by incense with Yin Yang symbol and smoky atmosphere.

To begin with Yii, you'll need to download the Yii framework and install it on your Windows machine.

Yii supports a wide range of databases, including MariaDB, which we'll be using in this tutorial.

First, make sure you have Composer installed on your Windows machine. Composer is a dependency manager for PHP, and Yii relies on it to manage its dependencies.

You can download Composer from the official website and follow the installation instructions to get it up and running on your machine.

Installation

To get started with Yii, you'll need to install Composer, which is a dependency manager and installer for PHP projects. This can be done by downloading and configuring WAMP on your Windows computer.

Composer requires PHP, which is located in the directory wamp64\bin\ in this example. You'll need to navigate to this location in the command prompt and run the following command: cd\wamp64\bin\php\php7.0.10.

To install Composer, use the command composer global require "fxp/composer-asset-plugin:^1.3.1". Be sure to check for the most current stable version, as software changes frequently.

For the most current versions of Composer and Composer Asset Plugin, you can check the following:

  • Composer
  • Composer asset plugin

Preparing Windows

PHP Screengrab
Credit: pexels.com, PHP Screengrab

To prepare your Windows computer for Yii, you'll need to download and configure WAMP. WAMP is a package that includes Apache, MySQL, and PHP, and it's a great way to set up a local server on your Windows computer.

You'll need to download WAMP and install it on your local computer, which the server will address as localhost. If you want to install it on a production Windows server, you'll need to contact your provider for server details and follow their recommendations.

For our purposes, we'll be installing WAMP on your local computer. Once you have WAMP installed, you'll need to install Composer, a dependency manager and installer for PHP projects.

To install Composer, you'll need to open the command prompt on Windows and navigate to the PHP folder. On my computer, this is located in the directory wamp64\bin\, where PHP version 7.0.10 is installed.

Here are the steps to install Composer:

  • Navigate to the PHP folder: `cd\wamp64\bin\php\php7.0.10`
  • Run the install command for the Composer Assets plugin: `composer global require "fxp/composer-asset-plugin:^1.3.1"`

Make sure to check the current stable version of Composer and the Composer Asset Plugin before installing, as software changes can affect compatibility.

Installing Advanced Template for Windows

Credit: youtube.com, yii2 installing advanced template manually without composer

To install the Yii Advanced template, you'll need to return to the wamp64\www directory and create a new directory called yii-advanced.

The command to install the Yii Advanced template is composer create-project --prefer-dist yiisoft/yii2-app-advanced yii-application.

You'll run this command in the new yii-advanced directory.

Manual Setup

To start, you need to install Codeception via Composer. This is the first step in setting up Codeception for your project.

You'll also need to create basic test suites, which will serve as the foundation for your testing framework.

To enable the Yii2 module for functional tests, you'll need to edit the functional.suite.yml file. The only required parameter for the Yii2 module is configFile, which should point to a test configuration file.

This test config file should be stored in the config folder of your application, and its path should be provided relatively to the codeception.yml file.

Make sure to set the YII_ENV constant to 'test' in the tests/_bootstrap.php file, as it is done in the basic and advanced app templates.

In these templates, the configFile is already defined in the global configuration file, so you won't need to provide a test config for each defined suite.

Database

Credit: youtube.com, insert data into database using form in yii 2

Creating a database is a straightforward process with Yii. You can use the SQL command line to create a database, and the command to do so is `create database yii2test;`. This will create a new database, and you can name it whatever you want.

To create a username and password for the database, you'll need to use the `GRANT ALL ON yiit2est.* TO 'your-username'@'localhost' IDENTIFIED BY 'your_chosen_password';` command. This will create a new user with all privileges, and you'll need to keep the username and password handy.

Here's a quick rundown of the process:

With Yii, you can easily switch between different database management systems (DBMS) using the PHP Data Objects (PDO) extension, which provides a uniform interface for different DBMS.

Creating MySQL Database

To create a MySQL database, you'll need to navigate to the MySQL folder in the Windows command line by typing `cd \wamp64\www\bin\mysql\mysql5.7.14\bin`. You can also do this from anywhere as long as WAMP or MAMP is in your local environment path.

Credit: youtube.com, Installing MySQL and Creating Databases | MySQL for Beginners

The next step is to type `mysql -u root -p` to bring up the mysql command line. From here, you can execute SQL commands to create your database. To do this, simply type `create database yii2test;` (or whatever name you choose for your database).

To create a username and password for your database, you'll need to use the following SQL commands: `GRANT ALL ON yii2test.* TO 'your-username'@'localhost' IDENTIFIED BY 'your_chosen_password';`, `FLUSH PRIVILEGES;`, and `EXIT;`. Don't forget to keep your database username and password handy, as you'll need them later to set up the PHP config file.

Here's a summary of the steps to create a MySQL database:

  • Navigate to the MySQL folder in the Windows command line
  • Type `mysql -u root -p` to bring up the mysql command line
  • Execute the SQL command `create database yii2test;` (or your chosen database name)
  • Create a username and password using the SQL commands `GRANT ALL`, `FLUSH PRIVILEGES`, and `EXIT`
  • Keep your database username and password on hand for future use

D.A.O

YII (DAO) is created on top of the PHP Data Objects (PDO) extension, allowing for permission to different databases management systems (DBMS) in a single uniform interface.

This means that applications using YII (DAO) can be easily switched to different DBMS without modifying the data access code.

Credit: youtube.com, DAO Design Pattern Explained: Introduction and Fundamentals | Data Access Object Design Pattern

The PDO extension provides a standardized way to interact with various DBMS, making it a convenient choice for developers.

This uniform interface simplifies the process of working with different databases, saving developers time and effort.

By abstracting away the complexities of database interactions, YII (DAO) enables developers to focus on building their applications rather than worrying about database specifics.

Connect to MariaDB

To connect to MariaDB, you'll need to use the mysql command-line tool or a graphical client like phpMyAdmin.

MariaDB uses a standard SQL syntax, so you can use any SQL client that supports it.

First, ensure that the MySQL service is running on your system. You can check this by looking for the process in your system's process list.

Next, open a terminal or command prompt and use the mysql command to connect to your MariaDB server.

Testing

Testing is a major advantage that Yii has. It has a built-in PHP testing framework called Codeception which simplifies tasks such as unit creation.

Credit: youtube.com, YII 2 Testing with Yii and Codeception(1)

Unit tests are located in the tests/unit directory and are supposed to contain all kinds of unit and integration testing. Each test case extends Codeception\Test\Unit class, which is standard Codeception format for unit testing.

Functional tests are used to test real features of web applications and are far more simpler than unit tests. They describe interaction scenario in a simple DSL so you don’t need to deal with application directly but describe actions from a user’s perspective.

API tests are not included in any Yii templates so you need to set up them manually if you are developing a web service. API testing is done at functional testing level but instead of testing HTML responses on user actions, they test requests and responses via protocols like REST or SOAP.

Acceptance tests test the user interaction with application but in this case using real browser and web server. They are much slower and much more fragile.

You can easily enable acceptance tests by renaming acceptance.suite.yml.example to acceptance.suite.yml. To run acceptance tests, you will need to launch application server in test mode and start a Selenium Server.

Frequently Asked Questions

What does Yii mean?

Yii is pronounced as "Yee" and means "simple and evolutionary" in Chinese. It can also be interpreted as "Yes It Is!" - a fitting motto for a powerful web development framework.

Is Yii still used?

Yes, Yii is still actively used by many websites and small businesses. Its popularity endures due to its reliability and ease of use

Is Yii similar to Laravel?

While both Yii and Laravel follow an MVC architecture, they differ in their approach to automation and code generation, with Laravel offering a more extensive command-line interface. Yii, on the other hand, provides features like ActiveRecord and a Gii code generator for streamlined development.

What is the Yii framework used for?

The Yii framework is a high-performance PHP framework for building complex web applications quickly, ideal for rapid development and maximum code reuse. It enables developers to create robust web applications efficiently.

Walter Brekke

Lead Writer

Walter Brekke is a seasoned writer with a passion for creating informative and engaging content. With a strong background in technology, Walter has established himself as a go-to expert in the field of cloud storage and collaboration. His articles have been widely read and respected, providing valuable insights and solutions to readers.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.