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

Simple Counter App - Symfony Typing CST Test

Loading…

Simple Counter App — Symfony Code

Demonstrates a simple Symfony controller and routes for a counter using session for state persistence.

<?php

// config/routes.yaml
counter_show:
	path: /counter
	controller: App\Controller\CounterController::show

counter_increment:
	path: /counter/increment
	controller: App\Controller\CounterController::increment

counter_decrement:
	path: /counter/decrement
	controller: App\Controller\CounterController::decrement

counter_reset:
	path: /counter/reset
	controller: App\Controller\CounterController::reset

// src/Controller/CounterController.php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class CounterController extends AbstractController {
	public function show(Request $request): Response {
		$count = $request->getSession()->get('count', 0);
		return $this->render('counter.html.twig', ['count' => $count]);
	}

	public function increment(Request $request): Response {
		$session = $request->getSession();
		$count = $session->get('count', 0) + 1;
		$session->set('count', $count);
		return $this->redirectToRoute('counter_show');
	}

	public function decrement(Request $request): Response {
		$session = $request->getSession();
		$count = $session->get('count', 0) - 1;
		$session->set('count', $count);
		return $this->redirectToRoute('counter_show');
	}

	public function reset(Request $request): Response {
		$request->getSession()->set('count', 0);
		return $this->redirectToRoute('counter_show');
	}
}

// templates/counter.html.twig
<!DOCTYPE html>
<html>
<head><title>Symfony Counter</title></head>
<body>
	<h2>Counter: {{ 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>
</body>
</html>

Symfony Language Guide

Symfony is a PHP web application framework and set of reusable components for building modern, scalable, and maintainable web applications and APIs.

Primary Use Cases

  • ▸Enterprise web applications
  • ▸RESTful APIs and microservices
  • ▸E-commerce platforms
  • ▸Content management systems
  • ▸Custom web platform development

Notable Features

  • ▸MVC architecture for structured development
  • ▸Reusable components and libraries
  • ▸Twig templating engine for clean views
  • ▸Doctrine ORM for database abstraction
  • ▸Event Dispatcher and service container for modular design

Origin & Creator

Created by Fabien Potencier in 2005, Symfony is maintained by SensioLabs and an active community of contributors.

Industrial Note

Symfony is popular in large-scale enterprise applications, complex web platforms, and projects requiring long-term maintainability, scalability, and strict code quality standards.

Quick Explain

  • ▸Symfony provides a structured architecture based on MVC (Model-View-Controller) principles.
  • ▸Includes reusable components for routing, forms, security, templating, caching, and more.
  • ▸Supports rapid development with built-in development tools, debug utilities, and profiler.
  • ▸Highly extensible via bundles, plugins, and third-party integrations.
  • ▸Widely used in enterprise-grade PHP applications and open-source projects.

Core Features

  • ▸Routing and controller system
  • ▸Form handling and validation
  • ▸Security system with authentication & authorization
  • ▸Caching and session management
  • ▸Logging, profiler, and debugging tools

Learning Path

  • ▸Learn PHP OOP and namespaces
  • ▸Understand MVC architecture
  • ▸Learn Symfony routing, controllers, and templates
  • ▸Explore services, dependency injection, and events
  • ▸Build small projects and increment complexity

Practical Examples

  • ▸Create a blog with CRUD functionality
  • ▸Build an e-commerce product catalog
  • ▸Develop a REST API for a mobile app
  • ▸Implement user authentication and role-based access
  • ▸Integrate third-party services via API clients

Comparisons

  • ▸Symfony vs Laravel: Symfony is more configurable and enterprise-ready; Laravel is simpler for rapid development
  • ▸Symfony vs Slim: Symfony is feature-rich; Slim is lightweight
  • ▸Symfony vs Zend/Laminas: Symfony has modern tooling and active community
  • ▸Symfony vs CakePHP: Symfony offers more flexibility and bundles
  • ▸Symfony vs WordPress: Symfony is a framework; WordPress is a CMS

Strengths

  • ▸High flexibility and modularity
  • ▸Strong community and ecosystem
  • ▸Enterprise-ready and long-term support versions
  • ▸Testable and maintainable codebase
  • ▸Integrates easily with other PHP libraries

Limitations

  • ▸Steep learning curve for beginners
  • ▸Configuration-heavy for small projects
  • ▸Can feel heavy for lightweight apps
  • ▸Performance tuning may be required for very large apps
  • ▸Some bundles may have version compatibility issues

When NOT to Use

  • ▸For extremely small/simple scripts
  • ▸When rapid prototyping is needed without setup
  • ▸If team is unfamiliar with PHP OOP and MVC
  • ▸When lightweight microframework is sufficient
  • ▸For static websites without backend logic

Cheat Sheet

  • ▸bin/console server:run - run local server
  • ▸bin/console make:controller - generate controller
  • ▸bin/console make:entity - generate entity
  • ▸bin/console debug:router - view routes
  • ▸bin/console cache:clear - clear cache

FAQ

  • ▸Is Symfony suitable for enterprise apps? -> Yes, it is widely used in large projects.
  • ▸Does Symfony support REST APIs? -> Yes, via controllers and API Platform.
  • ▸Is Symfony open-source? -> Yes, MIT license.
  • ▸What is a bundle in Symfony? -> Modular package containing code, templates, and services.
  • ▸How do I debug in Symfony? -> Use the profiler, logs, and debug toolbar.

30-Day Skill Plan

  • ▸Week 1: Install Symfony, explore CLI and basic controllers
  • ▸Week 2: Work with Doctrine ORM and forms
  • ▸Week 3: Implement services and dependency injection
  • ▸Week 4: Build REST API endpoints and test
  • ▸Week 5: Add security, authentication, and caching

Final Summary

  • ▸Symfony is a PHP framework for building modern web apps and APIs.
  • ▸Supports MVC, dependency injection, reusable components, and Twig templating.
  • ▸Ideal for enterprise projects requiring maintainability, scalability, and flexibility.
  • ▸Extensible via bundles and integrates with many third-party libraries.
  • ▸Provides robust tools for debugging, testing, and deployment.

Project Structure

  • ▸config/ - application configuration
  • ▸src/ - PHP classes including controllers and services
  • ▸templates/ - Twig templates
  • ▸public/ - web-accessible assets (CSS, JS, images)
  • ▸var/ - cache, logs, and temporary files

Monetization

  • ▸Open-source Symfony is free
  • ▸Commercial support via SensioLabs
  • ▸Training and certification services
  • ▸Enterprise bundles for specific business needs
  • ▸Consulting for large-scale Symfony deployments

Productivity Tips

  • ▸Use Symfony Flex to simplify bundle installation
  • ▸Leverage MakerBundle to generate boilerplate code
  • ▸Keep services small and focused
  • ▸Use environment variables for config
  • ▸Use profiler and debug tools to accelerate development

Basic Concepts

  • ▸Bundle - modular package containing code, templates, and services
  • ▸Controller - handles HTTP requests and returns responses
  • ▸Service - reusable PHP object managed by the container
  • ▸Routing - maps URLs to controllers
  • ▸Entity - represents a database record using Doctrine ORM

Official Docs

  • ▸https://symfony.com/doc/current/index.html
  • ▸Symfony GitHub repository
  • ▸Symfony Slack, forums, and community resources

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher