Simple Counter App - Laravel Typing CST Test
Loading…
Simple Counter App — Laravel Code
Demonstrates a simple Laravel controller and routes for a counter using session for state persistence.
<?php
// routes/web.php
use IlluminateSupportFacadesRoute;
use AppHttpControllers\CounterController;
Route::get('/counter', [CounterController::class, 'show']);
Route::post('/counter/increment', [CounterController::class, 'increment']);
Route::post('/counter/decrement', [CounterController::class, 'decrement']);
Route::post('/counter/reset', [CounterController::class, 'reset']);
// app/Http/Controllers/CounterController.php
namespace AppHttpControllers;
use IlluminateHttpRequest;
class CounterController extends Controller {
public function show(Request $request) {
$count = $request->session()->get('count', 0);
return view('counter', ['count' => $count]);
}
public function increment(Request $request) {
$count = $request->session()->get('count', 0) + 1;
$request->session()->put('count', $count);
return redirect('/counter');
}
public function decrement(Request $request) {
$count = $request->session()->get('count', 0) - 1;
$request->session()->put('count', $count);
return redirect('/counter');
}
public function reset(Request $request) {
$request->session()->put('count', 0);
return redirect('/counter');
}
}
// resources/views/counter.blade.php
<!DOCTYPE html>
<html>
<head><title>Laravel Counter</title></head>
<body>
<h2>Counter: {{ $count }}</h2>
<form method='POST' action='/counter/increment'>@csrf<button type='submit'>+</button></form>
<form method='POST' action='/counter/decrement'>@csrf<button type='submit'>-</button></form>
<form method='POST' action='/counter/reset'>@csrf<button type='submit'>Reset</button></form>
</body>
</html>Laravel Language Guide
Laravel is an open-source PHP web framework designed for building modern, scalable, and maintainable web applications with expressive syntax, following MVC architecture and emphasizing developer productivity.
Primary Use Cases
- ▸Building modern web applications
- ▸Creating RESTful APIs
- ▸Developing SaaS platforms
- ▸Implementing authentication and authorization systems
- ▸Rapid prototyping and MVP development
Notable Features
- ▸Eloquent ORM for database abstraction
- ▸Blade templating engine
- ▸Routing and middleware support
- ▸Artisan CLI for automation tasks
- ▸Built-in authentication, authorization, and security features
Origin & Creator
Created by Taylor Otwell in 2011 and maintained by the Laravel community.
Industrial Note
Laravel is widely used in small-to-large web applications, SaaS platforms, and API-driven systems where rapid development and maintainable code are essential.
Quick Explain
- ▸Laravel simplifies common tasks like routing, authentication, sessions, caching, and database interactions.
- ▸It uses the MVC (Model-View-Controller) design pattern for structured, maintainable code.
- ▸Includes an ORM called Eloquent for easy database operations.
- ▸Provides built-in tools for testing, queues, events, and API development.
- ▸Supports blade templating engine for dynamic, clean views and components.
Core Features
- ▸MVC architecture
- ▸Routing and controllers
- ▸Eloquent ORM and database migrations
- ▸Blade templating
- ▸Queues, events, and task scheduling
Learning Path
- ▸Week 1: PHP basics and MVC concepts
- ▸Week 2: Routing, controllers, and views
- ▸Week 3: Eloquent ORM and database migrations
- ▸Week 4: Authentication, middleware, and security
- ▸Week 5: Queues, events, API development, and testing
Practical Examples
- ▸Building a blog with authentication and comments
- ▸Developing a REST API for a mobile app
- ▸Implementing user roles and permissions
- ▸Scheduling tasks using Laravel scheduler
- ▸Building a real-time chat app using broadcasting
Comparisons
- ▸Laravel vs Symfony -> Laravel is more opinionated and faster to start; Symfony is more modular
- ▸Laravel vs CodeIgniter -> Laravel has modern features and Eloquent ORM; CI is lightweight
- ▸Laravel vs CakePHP -> Laravel offers better modern tooling and community support
- ▸Laravel vs Yii -> Laravel has expressive syntax and large ecosystem
- ▸Laravel vs Node.js frameworks -> Laravel is PHP-based and opinionated; Node.js offers JavaScript runtime flexibility
Strengths
- ▸Expressive and clean syntax for rapid development
- ▸Strong community and ecosystem
- ▸Scalable for small apps to enterprise solutions
- ▸Built-in security and testing features
- ▸Extensible via packages and modules
Limitations
- ▸Performance can be lower than micro-frameworks for extremely high-load apps
- ▸Monolithic structure may be overkill for very small projects
- ▸Requires PHP knowledge and modern PHP versions
- ▸Learning curve for advanced features like queues and broadcasting
- ▸Dependent on PHP runtime and environment setup
When NOT to Use
- ▸Very small static websites without dynamic features
- ▸Projects not using PHP
- ▸Applications needing extremely low-latency microservices
- ▸When you want a zero-dependency lightweight framework
- ▸Projects where learning curve for MVC is prohibitive
Cheat Sheet
- ▸php artisan make:controller -> create a controller
- ▸php artisan make:model -> create a model
- ▸php artisan migrate -> run database migrations
- ▸php artisan serve -> start development server
- ▸php artisan tinker -> interactive console for testing
FAQ
- ▸Is Laravel free? -> Yes, open-source under MIT License
- ▸Does Laravel require PHP? -> Yes, PHP 8+ recommended
- ▸Can Laravel be used for APIs? -> Yes, with API routes and Sanctum/Passport
- ▸Does Laravel support multiple databases? -> Yes, via Eloquent configuration
- ▸Is Laravel suitable for enterprise apps? -> Yes, with proper architecture and scaling
30-Day Skill Plan
- ▸Master Eloquent ORM relationships and queries
- ▸Use Blade components and layouts effectively
- ▸Implement authentication and authorization patterns
- ▸Optimize performance with caching and queues
- ▸Write robust tests and integrate CI/CD
Final Summary
- ▸Laravel is a modern PHP framework for web applications and APIs.
- ▸Follows MVC architecture with expressive syntax.
- ▸Includes Eloquent ORM, Blade templates, queues, events, and CLI tooling.
- ▸Supports testing, security, and scalable application development.
- ▸Extensible and widely supported for small projects to enterprise-grade apps.
Project Structure
- ▸app/ - core application code (Models, Controllers, Services)
- ▸resources/ - views (Blade templates), assets, translations
- ▸routes/ - route definitions
- ▸database/ - migrations, seeders, factories
- ▸config/ - application configuration files
Monetization
- ▸Develop SaaS platforms
- ▸Build custom web applications for clients
- ▸Offer Laravel consulting services
- ▸Create premium packages or extensions
- ▸Training and workshops for Laravel developers
Productivity Tips
- ▸Use Artisan commands for scaffolding
- ▸Leverage Eloquent relationships for database efficiency
- ▸Use Blade components and layouts
- ▸Cache frequently used data
- ▸Automate testing and CI/CD pipelines
Basic Concepts
- ▸Routes - map URLs to controllers or closures
- ▸Controllers - handle request logic
- ▸Models - represent database tables with Eloquent
- ▸Views - Blade templates for rendering HTML
- ▸Migrations - version control for database schema
Official Docs
- ▸https://laravel.com/docs
- ▸Laravel GitHub Repository
- ▸Laravel News and Ecosystem resources