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.