Catalog / Yii Framework Cheatsheet
Yii Framework Cheatsheet
A concise reference guide to the Yii PHP framework, covering core components, commonly used features, and best practices for efficient web application development.
Core Concepts & Architecture
MVC Structure
Model |
Represents data and business logic. Interacts with the database. |
View |
Presents the data to the user. Consists of HTML, CSS, and PHP code for display. |
Controller |
Handles user requests, interacts with models, and selects views to render. |
Entry Script (index.php) |
The single entry point for all web requests. Initializes the application. |
Application |
The central object that manages the overall execution flow. |
Components |
Reusable modules providing specific functionalities (e.g., database, session, user). |
Application Lifecycle
|
Configuration
Configuration Array |
Yii applications are configured using a PHP array, typically located in |
Components Configuration |
Configures core application components such as |
Modules Configuration |
Defines modules and their specific configurations. |
Parameters Configuration |
Defines global application parameters accessible throughout the application. |
Example |
|
Database Interaction
Active Record
Active Record (AR) provides an object-oriented interface for accessing and manipulating data stored in databases. Each AR class represents a database table, and an AR instance represents a row in that table. |
Defining an AR Class
|
Basic CRUD Operations |
|
Query Builder
The Query Builder provides a programmatic and database-agnostic way to construct SQL queries. |
Example:
|
Chaining Methods: The Query Builder allows you to chain methods to build complex queries easily. |
Migrations
Creating a Migration |
|
Applying Migrations |
|
Reverting Migrations |
|
Migration Class Structure |
|
Working with Views & Controllers
Rendering Views
Rendering a Simple View |
|
Rendering a View with Layout |
|
Rendering a Partial View |
|
Accessing Variables in Views |
Variables passed to the |
Controller Actions
Controller actions are methods within a controller class that handle specific user requests. They typically perform tasks such as loading data, processing user input, and rendering views. |
Action Naming Convention: Action names should start with the word |
Example:
|
Layouts
Main Layout |
The default layout file, typically located in |
Layout Structure |
Layout files typically contain HTML |
Rendering Content in Layout |
The |
Forms and Input Validation
Creating Forms
Forms in Yii are typically created using the |
Example:
|
Input Validation
Validation Rules |
Define validation rules in the model’s |
Common Validators |
|
Example: |
|
Handling Form Submission
In the controller action, check if the form has been submitted and if the model is valid. If so, process the data and redirect the user. |
Example:
|