Yii Framework

PHP Yii Framework
Yii is a high-performance PHP framework known for its speed, security, and extensibility. It provides developers with a solid foundation for building web applications quickly and efficiently. In this article, we'll explore how to get started with Yii and dive into its core components.

Getting Started with Yii

Installation

To begin using Yii, you’ll first need to install it via Composer, a dependency manager for PHP. Open your terminal and run the following command:

				
					composer create-project --prefer-dist yiisoft/yii2-app-basic my-yii-app

				
			

This command will create a new Yii project named my-yii-app in the current directory. Composer will download all the necessary dependencies and set up the project structure for you.

Development Server

After creating a new Yii project, you can use the built-in development server to run your application locally. Navigate to the project directory and run the following command:

				
					php yii serve

				
			

This command will start the Yii development server, and you can access your Yii application by visiting http://localhost:8080 in your web browser.

Configuration

Yii’s configuration files are located in the config directory of your project. Here you can configure various aspects of your application, such as database connections, routing rules, and error handling settings.

The main configuration file is config/web.php, where you can define global settings for your web application:

				
					<?php

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';

$config = [
    'id' => 'basic',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'components' => [
        'request' => [
            'cookieValidationKey' => 'your-secret-key',
        ],
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
        ],
        'errorHandler' => [
            'errorAction' => 'site/error',
        ],
        'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'useFileTransport' => true,
        ],
        'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
        'db' => $db,
    ],
    'params' => $params,
];

return $config;

				
			

Yii Components

Yii is built on top of a collection of reusable PHP components, which can be used independently in any PHP project. Let’s explore some of the core Yii components:

ActiveRecord

The ActiveRecord component provides an object-relational mapping (ORM) framework for interacting with databases. It allows you to represent database tables as PHP objects and perform CRUD (Create, Read, Update, Delete) operations on them.

				
					use yii\db\ActiveRecord;

class User extends ActiveRecord
{
}

				
			

ActiveForm

The ActiveForm component simplifies the process of creating HTML forms and handling form submissions. It provides a fluent interface for defining form elements, validation rules, and error messages.

				
					use yii\widgets\ActiveForm;

$form = ActiveForm::begin();
echo $form->field($model, 'username');
echo $form->field($model, 'password')->passwordInput();
echo $form->field($model, 'rememberMe')->checkbox();
echo Html::submitButton('Login', ['class' => 'btn btn-primary']);
ActiveForm::end();

				
			

GridView

The GridView component allows you to display tabular data in a grid format with pagination, sorting, and filtering capabilities. It provides a flexible and customizable interface for working with large datasets.

				
					use yii\grid\GridView;

echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        'id',
        'name',
        'email',
        [
            'class' => 'yii\grid\ActionColumn',
            'template' => '{view} {update} {delete}',
        ],
    ],
]);

				
			

ActiveForm

The ActiveForm component simplifies the process of creating HTML forms and handling form submissions. It provides a fluent interface for defining form elements, validation rules, and error messages.

				
					use yii\widgets\ActiveForm;

$form = ActiveForm::begin();
echo $form->field($model, 'username');
echo $form->field($model, 'password')->passwordInput();
echo $form->field($model, 'rememberMe')->checkbox();
echo Html::submitButton('Login', ['class' => 'btn btn-primary']);
ActiveForm::end();

				
			

Gii

Gii is a powerful code generation tool that helps you generate model, controller, CRUD, and other code files quickly and easily. It provides a web-based interface for generating code based on database tables and scaffolding.

				
					php yii gii/model --tableName=tableName

				
			

Console

The Console component provides a console application framework for running command-line scripts. It allows you to define and execute console commands in a structured and efficient manner.

				
					php yii controller/action

				
			

Conclusion

In this article, we’ve covered the basics of getting started with Yii and explored its core components. Yii’s simplicity, speed, and extensibility make it an excellent choice for building web applications of any size or complexity. Whether you’re a beginner or an experienced developer, Yii provides the tools and flexibility you need to succeed in your PHP projects.

Scroll to Top