Zend Framework Simple Counter App - Zend-framework Typing CST Test
Loading…
Zend Framework Simple Counter App — Zend-framework Code
Demonstrates a simple Zend Framework controller and routes for a counter using session for state persistence.
<?php
// module/Application/config/module.config.php
return [
'router' => [
'routes' => [
'counter' => [
'type' => 'Literal',
'options' => [
'route' => '/counter',
'defaults' => [
'controller' => Application\Controller\CounterController::class,
'action' => 'index',
],
],
],
],
],
];
// module/Application/src/Controller/CounterController.php
namespace Application\Controller;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
class CounterController extends AbstractActionController {
public function indexAction() {
$session = new \Laminas\Session\Container('counter');
if (!isset($session->count)) { $session->count = 0; }
return new ViewModel(['count' => $session->count]);
}
public function incrementAction() {
$session = new \Laminas\Session\Container('counter');
$session->count = ($session->count ?? 0) + 1;
return $this->redirect()->toRoute('counter');
}
public function decrementAction() {
$session = new \Laminas\Session\Container('counter');
$session->count = ($session->count ?? 0) - 1;
return $this->redirect()->toRoute('counter');
}
public function resetAction() {
$session = new \Laminas\Session\Container('counter');
$session->count = 0;
return $this->redirect()->toRoute('counter');
}
}
// module/Application/view/application/counter/index.phtml
<h2>Counter: <?= $this->count ?></h2>
<form action='/counter/increment' method='post'><button type='submit'>+</button></form>
<form action='/counter/decrement' method='post'><button type='submit'>-</button></form>
<form action='/counter/reset' method='post'><button type='submit'>Reset</button></form>Zend-framework Language Guide
Zend Framework (now Laminas Project) is a PHP-based open-source framework for building enterprise-ready web applications and APIs using a modular, object-oriented architecture with emphasis on performance, security, and extensibility.
Primary Use Cases
- ▸Enterprise web application development
- ▸Building RESTful and SOAP APIs
- ▸Modular component-based architectures
- ▸Middleware and service-oriented PHP applications
- ▸Applications requiring security, caching, and logging
Notable Features
- ▸MVC architecture
- ▸Modular, decoupled components
- ▸Service manager for dependency injection
- ▸Event-driven architecture
- ▸Built-in security, authentication, and caching components
Origin & Creator
Originally created by Zend Technologies in 2005, now maintained as the Laminas Project after transitioning from Zend Framework.
Industrial Note
Widely used in enterprise PHP applications, APIs, and middleware solutions where modularity, security, and maintainability are critical.
Quick Explain
- ▸Zend Framework provides a set of decoupled components that can be used independently or together.
- ▸Supports MVC architecture for structuring web applications.
- ▸Focuses on enterprise-grade features like authentication, caching, and validation.
- ▸Highly extensible with event-driven architecture and service managers.
- ▸Encourages best practices with object-oriented programming, PSR standards, and dependency injection.
Core Features
- ▸Routing - map URLs to controllers and actions
- ▸Controllers & Actions - handle HTTP requests
- ▸View & Template system - generate HTML responses
- ▸Forms & Input validation - secure user input handling
- ▸Service Manager - centralized dependency injection container
Learning Path
- ▸Learn basic and advanced PHP OOP
- ▸Understand Composer and autoloading
- ▸Study MVC architecture
- ▸Learn Laminas/Zend components
- ▸Build small apps, then modular enterprise apps
Practical Examples
- ▸Corporate internal portal
- ▸REST API for mobile apps
- ▸E-commerce backend with modular services
- ▸Middleware for data transformation and logging
- ▸Authentication and authorization system
Comparisons
- ▸Zend vs Laravel: Enterprise modular vs rapid development
- ▸Zend vs Symfony: Component-based vs full-stack structured
- ▸Zend vs Slim: Heavyweight enterprise vs lightweight micro-framework
- ▸Zend vs CodeIgniter: More configuration and flexibility vs simplicity
- ▸Zend vs Yii: Enterprise features vs high-performance scaffolding
Strengths
- ▸Highly modular and extensible
- ▸Enterprise-ready with security and caching
- ▸Supports both small apps and large-scale systems
- ▸Adheres to PHP-FIG PSR standards
- ▸Decoupled components allow selective usage
Limitations
- ▸Steeper learning curve for beginners
- ▸Heavier than micro-frameworks like Slim
- ▸Requires more configuration for small apps
- ▸Not as popular as Laravel in community support
- ▸Slower adoption for rapid prototyping
When NOT to Use
- ▸Simple, small-scale applications
- ▸Rapid prototyping projects
- ▸Developers new to PHP frameworks
- ▸Apps not requiring enterprise-grade security or modularity
- ▸Projects with tight deadlines favoring lightweight frameworks
Cheat Sheet
- ▸$this->params()->fromQuery('key') - get query parameter
- ▸$this->redirect()->toRoute('routeName') - redirect
- ▸new ViewModel(['var' => value]) - render data in view
- ▸$serviceManager->get('ServiceName') - retrieve service
- ▸return $this->json(['key' => 'value']) - send JSON response
FAQ
- ▸Is Zend Framework still maintained? -> Yes, as Laminas Project.
- ▸Can I use components individually? -> Yes, Zend components are modular.
- ▸Is Zend suitable for small apps? -> Not ideal; use Slim or Silex.
- ▸Does Zend enforce MVC? -> MVC is recommended but components can be used standalone.
- ▸Is it open-source? -> Yes, under the BSD license (Laminas: MIT).
30-Day Skill Plan
- ▸Week 1: Basic routing and controllers
- ▸Week 2: Views, templates, and forms
- ▸Week 3: Service Manager and dependency injection
- ▸Week 4: Event-driven programming and modules
- ▸Week 5: Deploy and test a complete Zend application
Final Summary
- ▸Zend Framework (Laminas Project) is a modular, enterprise-grade PHP framework.
- ▸Supports MVC architecture, dependency injection, and event-driven development.
- ▸Ideal for large-scale applications, APIs, and modular enterprise solutions.
- ▸Highly extensible with decoupled components.
- ▸Focuses on security, performance, and maintainable PHP code.
Project Structure
- ▸module/ - contains application modules
- ▸config/ - configuration files (routes, services, etc.)
- ▸public/ - web root for index.php and assets
- ▸vendor/ - Composer dependencies
- ▸composer.json - project dependencies and autoloading
Basic Concepts
- ▸Module - self-contained application component
- ▸Controller - handles HTTP requests
- ▸Action - method in controller responding to a route
- ▸Service Manager - dependency injection container
- ▸Event Manager - decoupled event handling system
Official Docs
- ▸https://docs.laminas.dev/
- ▸Laminas GitHub repository
- ▸Laminas MVC Documentation
- ▸Composer and Laminas components documentation
- ▸Laminas API Tools Documentation