- Yii2 Framework
-
Introduction & Setup
- Introduction to Yii2 Framework
- Installing Yii2 (Basic & Advanced Templates)
- Understanding Yii2 Directory Structure
- Yii2 Configuration Basics
- Routing & Pretty URLs in Yii2
-
Yii2 Core Concepts
- Yii2 Application Bootstrapping & Lifecycle
- Understanding Yii2 Request & Response Handling
- Working with Yii2 Components & Helpers
- Yii2 Widgets & Using Built-in Widgets
- Yii2 Helpers & Utility Classes
-
Models & Database Operations
- Yii2 Models, Active Record & Database Connections
- CRUD Operations in Yii2
- Yii2 Query Builder & DAO (Direct SQL Queries)
- Handling Relationships in Yii2 Active Record
- Yii2 Migrations & Seeding
-
Views, Layouts & Themes
- Yii2 Views & Layouts
- Yii2 Asset Bundles & Asset Management
- Integrating Bootstrap in Yii2
- Yii2 Theme Integration
- Yii2 Custom Widgets & Reusable Components
-
Forms, Validation & Data Presentation
- Yii2 Forms & Validation
- Using Yii2 GridView & ListView Widgets
- Yii2 Pagination & Sorting
- Yii2 File Uploads
-
Security & User Management
- User Authentication in Yii2
- Role-Based Access Control (RBAC) in Yii2
- Yii2 Security Features
-
Console Commands & Advanced Features
- Yii2 Console Commands
- Yii2 Events & Behaviors
- Yii2 RESTful API Development
- Consuming Third-Party APIs in Yii2
- Yii2 Background Jobs & Queue System
-
Performance Optimization & Caching
- Yii2 Caching Techniques
- Yii2 Performance Optimization
- Debugging & Logging in Yii2
-
Deployment & Best Practices
- Deploying Yii2 Applications
- Yii2 Best Practices & Large-Scale Application Structure
- Yii2 Multilingual & Localization Support
- Yii2 Module Development
- Integrating Yii2 with Frontend Frameworks (Angular/Vue/React)
-
Special Topics
- Dependency Injection (DI) in Yii2
Yii2 Configuration Basics
Introduction
Yii2 provides a flexible and structured configuration system that allows developers to manage application settings efficiently. Configuration files in Yii2 define database connections, components, modules, behaviors, and application-specific parameters. Understanding how configuration works is crucial for customizing and optimizing your Yii2 application.
1. Yii2 Configuration Files
Yii2 stores configuration settings in PHP array files, primarily located in the config/
directory. The key configuration files are:
For Basic Template:
config/web.php
→ Web application settingsconfig/console.php
→ Console application settingsconfig/db.php
→ Database configuration
For Advanced Template:
common/config/main.php
→ Common settings for frontend & backendcommon/config/main-local.php
→ Local overrides (e.g., database credentials)frontend/config/main.php
→ Frontend-specific settingsbackend/config/main.php
→ Backend-specific settingsconsole/config/main.php
→ Console application settings
2. Setting Up Database Connection
Basic Template (config/db.php
):
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dynamic_duniya',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
];
Advanced Template (common/config/main-local.php
):
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dynamic_duniya',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
],
],
];
Note: Advanced Template uses main-local.php
for local configurations.
3. Configuring Components
Yii2 allows configuring application components in the components
section of the configuration file.
Example: Configuring the mailer component in web.php
(Basic Template)
'components' => [
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => true, // Change to false to enable real email sending
],
],
4. Setting Application Parameters
Yii2 applications can have custom parameters defined in params.php
.
Example (config/params.php
in Basic Template)
return [
'adminEmail' => 'admin@dynamicduniya.com',
'siteName' => 'Dynamic Duniya',
];
To access parameters in the code:
Yii::$app->params['adminEmail'];
5. URL Management and Pretty URLs
By default, Yii2 uses query parameters in URLs, but we can enable pretty URLs using URL Manager.
Enable Pretty URLs (config/web.php
)
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
],
],
],
Now, instead of:
http://localhost/index.php?r=site/contact
You can use:
http://localhost/site/contact
6. Configuring Error Handling
Yii2 provides built-in error handling through the errorHandler
component. By default, Yii2 already has an error action configured in SiteController
.
Enable Error Page (config/web.php
in Basic Template)
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
],
By default, Yii2 has an error.php
view in views/site/error.php
that displays error messages. If needed, you can customize this file for a better user experience.
Customize Error Page (config/web.php
in Basic Template)
'components' => [
'errorHandler' => [
'errorAction' => 'site/error',
],
],
Create the error action in SiteController.php
:
public function actionError()
{
$exception = Yii::$app->errorHandler->exception;
if ($exception !== null) {
return $this->render('error', ['exception' => $exception]);
}
}
7. Caching Configuration
Yii2 supports various caching mechanisms like file caching, database caching, and Redis/Memcached.
Example: Enable file caching (config/web.php
):
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
],
8. Configuring Session and Cookies
To configure session management, modify the session
component:
'components' => [
'session' => [
'class' => 'yii\web\Session',
'timeout' => 86400, // 1-day session timeout
],
],
To configure cookies:
'components' => [
'response' => [
'class' => 'yii\web\Response',
'on beforeSend' => function ($event) {
Yii::$app->response->cookies->add(new \yii\web\Cookie([
'name' => 'test_cookie',
'value' => 'DynamicDuniya',
'expire' => time() + 3600, // 1 hour
]));
},
],
],
Conclusion
Yii2’s configuration system is flexible and modular, allowing developers to customize every aspect of their application. In this tutorial, we covered database connections, components, URL management, caching, and error handling.
Next Tutorial:
In the next tutorial, we will explore Routing and Controllers in Yii2 to understand how requests are handled.
Prepare for Interview
- Debugging in Python
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
- Generators in Python
- Requests in Python
- Django
- Flask
- Matplotlib/Seaborn
- Pandas
- NumPy
- Modules and Packages in Python
- File Handling in Python
- Error Handling and Exceptions in Python
- Indexing and Performance Optimization in SQL
Random Blogs
- How to Start Your Career as a DevOps Engineer
- Downlaod Youtube Video in Any Format Using Python Pytube Library
- Variable Assignment in Python
- Understanding AI, ML, Data Science, and More: A Beginner's Guide to Choosing Your Career Path
- The Ultimate Guide to Starting a Career in Computer Vision
- Understanding OLTP vs OLAP Databases: How SQL Handles Query Optimization
- Data Analytics: The Power of Data-Driven Decision Making
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- Datasets for Exploratory Data Analysis for Beginners
- 5 Ways Use Jupyter Notebook Online Free of Cost
- 10 Awesome Data Science Blogs To Check Out
- What is YII? and How to Install it?
- Store Data Into CSV File Using Python Tkinter GUI Library
- Google’s Core Update in May 2020: What You Need to Know
- Datasets for analyze in Tableau
Datasets for Machine Learning
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset
- Artificial Characters Dataset
- Bitcoin Heist Ransomware Address Dataset