1. Home
  2. /
  3. Yii
  4. /
  5. Simple Counter App

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.

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher