Simple Counter App - Yii Typing CST Test
Loading…
Simple Counter App — Yii Code
Demonstrates a simple Yii controller and view for a counter using session for state persistence.
<?php
// controllers/CounterController.php
namespace app\controllers;
use Yii;
use yii\web\Controller;
class CounterController extends Controller {
public function actionIndex() {
$session = Yii::$app->session;
if (!$session->has('count')) { $session->set('count', 0); }
$count = $session->get('count');
return $this->render('index', ['count' => $count]);
}
public function actionIncrement() {
$session = Yii::$app->session;
$session->set('count', $session->get('count', 0) + 1);
return $this->redirect(['index']);
}
public function actionDecrement() {
$session = Yii::$app->session;
$session->set('count', $session->get('count', 0) - 1);
return $this->redirect(['index']);
}
public function actionReset() {
Yii::$app->session->set('count', 0);
return $this->redirect(['index']);
}
}
// views/counter/index.php
<h2>Counter: <?= $count ?></h2>
<form method='post' action='increment'><button type='submit'>+</button></form>
<form method='post' action='decrement'><button type='submit'>-</button></form>
<form method='post' action='reset'><button type='submit'>Reset</button></form>Yii Language Guide
Yii is a high-performance, component-based PHP framework for rapidly developing modern web applications. It emphasizes performance, security, and extensibility, following the MVC pattern and providing tools for both small and large-scale applications.
Primary Use Cases
- ▸Building modern web applications
- ▸Creating RESTful APIs
- ▸Developing enterprise-grade platforms
- ▸Implementing authentication and RBAC
- ▸Rapid prototyping with Gii code generator
Notable Features
- ▸Gii code generator for CRUD scaffolding
- ▸Active Record ORM for database operations
- ▸RESTful API support
- ▸Built-in caching, authentication, and RBAC
- ▸Component-based architecture for modular development
Origin & Creator
Created by Qiang Xue in 2008 and maintained by the Yii Software LLC and the Yii community.
Industrial Note
Yii is widely used in enterprise applications, e-commerce platforms, CMS systems, and APIs where high performance and robust architecture are essential.
Quick Explain
- ▸Yii provides an MVC architecture for structured, maintainable code.
- ▸Includes Gii, a powerful code generator for CRUD operations and scaffolding.
- ▸Supports Active Record for database interactions.
- ▸Offers built-in tools for authentication, RBAC, caching, and RESTful APIs.
- ▸Provides easy integration with frontend frameworks and widgets.
Core Features
- ▸MVC architecture
- ▸Routing and controllers
- ▸Active Record and database migrations
- ▸Widgets and theming for UI
- ▸Events and behaviors for extensibility
Learning Path
- ▸Week 1: PHP basics and MVC fundamentals
- ▸Week 2: Routing, controllers, and views
- ▸Week 3: Active Record and database migrations
- ▸Week 4: Authentication, RBAC, and caching
- ▸Week 5: REST APIs, console commands, and Gii scaffolding
Practical Examples
- ▸Building a CMS with authentication and role management
- ▸Developing a RESTful API for mobile apps
- ▸Implementing e-commerce product management
- ▸Creating dashboards with widgets and charts
- ▸Scheduling tasks using console commands
Comparisons
- ▸Yii vs Laravel -> Yii is faster and more component-based; Laravel has expressive syntax
- ▸Yii vs Symfony -> Yii is simpler and faster; Symfony is more modular and enterprise-focused
- ▸Yii vs CodeIgniter -> Yii is more feature-rich and secure; CI is lightweight
- ▸Yii vs CakePHP -> Yii provides better performance and modern tools
- ▸Yii vs Zend Framework -> Yii is higher-performance and developer-friendly
Strengths
- ▸High-performance and fast execution
- ▸Strong security features (CSRF, XSS, SQL injection prevention)
- ▸Powerful code generation via Gii
- ▸Flexible, extensible, and component-based
- ▸Good for both small projects and enterprise solutions
Limitations
- ▸Smaller community compared to Laravel or Symfony
- ▸Steeper learning curve for beginners
- ▸Less modern syntax than Laravel in some areas
- ▸Requires careful configuration for very large projects
- ▸Dependent on PHP environment and extensions
When NOT to Use
- ▸Very small static websites without dynamic content
- ▸Projects not using PHP
- ▸Applications requiring extremely lightweight micro-frameworks
- ▸When rapid prototyping is not needed
- ▸Projects where team lacks PHP MVC knowledge
Cheat Sheet
- ▸php yii serve -> start development server
- ▸php yii gii -> generate controllers, models, CRUD
- ▸php yii migrate -> run database migrations
- ▸php yii cache/flush-all -> clear cache
- ▸php yii test -> run tests with Codeception
FAQ
- ▸Is Yii free? -> Yes, open-source under BSD License
- ▸Does Yii require PHP? -> Yes, PHP 8+ recommended
- ▸Can Yii be used for APIs? -> Yes, with RESTful modules
- ▸Does Yii support multiple databases? -> Yes, via Active Record configuration
- ▸Is Yii suitable for enterprise apps? -> Yes, with proper modular design and caching
30-Day Skill Plan
- ▸Master Active Record relationships and queries
- ▸Use widgets and layouts efficiently
- ▸Implement authentication and RBAC patterns
- ▸Optimize performance with caching and eager loading
- ▸Write robust tests and integrate CI/CD
Final Summary
- ▸Yii is a high-performance PHP framework for web applications and APIs.
- ▸Follows MVC and component-based architecture.
- ▸Includes Active Record, Gii code generator, widgets, RBAC, and caching tools.
- ▸Supports testing, security, and scalable application development.
- ▸Extensible and suitable for small projects to enterprise-grade systems.
Project Structure
- ▸controllers/ - controller classes
- ▸models/ - Active Record and domain models
- ▸views/ - templates and layout files
- ▸config/ - application configuration
- ▸runtime/ - logs, cache, and temporary files
Monetization
- ▸Develop SaaS platforms
- ▸Build enterprise web applications
- ▸Offer Yii consulting services
- ▸Create premium modules or extensions
- ▸Training and workshops for Yii developers
Productivity Tips
- ▸Use Gii to speed up CRUD development
- ▸Leverage Active Record relationships for database efficiency
- ▸Use widgets and layouts for reusable UI
- ▸Cache frequently used data
- ▸Automate testing and CI/CD pipelines
Basic Concepts
- ▸Routes - map URLs to controllers and actions
- ▸Controllers - handle request processing
- ▸Models - represent database tables using Active Record
- ▸Views - render UI using PHP templates or widgets
- ▸Migrations - version control for database schema
Official Docs
- ▸https://www.yiiframework.com/doc/guide/2.0/en
- ▸Yii GitHub Repository
- ▸Yii Community Wiki and Tutorials