Simple Todo App - Phoenix Typing CST Test
Loading…
Simple Todo App — Phoenix Code
Demonstrates a Phoenix controller and view for managing Todo items with a simple JSON API.
# lib/my_app_web/controllers/todo_controller.ex
defmodule MyAppWeb.TodoController do
use MyAppWeb, :controller
alias MyApp.Todos
def index(conn, _params) do
todos = Todos.list_todos()
json(conn, todos)
end
def create(conn, %{"todo" => todo_params}) do
{:ok, todo} = Todos.create_todo(todo_params)
json(conn, todo)
end
end
# lib/my_app/todos/todos.ex
defmodule MyApp.Todos do
alias MyApp.Repo
alias MyApp.Todos.Todo
def list_todos do
Repo.all(Todo)
end
def create_todo(attrs) do
%Todo{}
|> Todo.changeset(attrs)
|> Repo.insert()
end
end
# lib/my_app/todos/todo.ex
defmodule MyApp.Todos.Todo do
use Ecto.Schema
import Ecto.Changeset
schema "todos" do
field :title, :string
field :completed, :boolean, default: false
timestamps()
end
def changeset(todo, attrs) do
todo
|> cast(attrs, [:title, :completed])
|> validate_required([:title])
end
endPhoenix Language Guide
Phoenix is a high-performance, functional web framework written in Elixir, designed for building scalable and maintainable web applications and real-time systems.
Primary Use Cases
- ▸Real-time web applications (chat, notifications, dashboards)
- ▸RESTful APIs and JSON backends
- ▸Fault-tolerant, scalable systems
- ▸High-concurrency microservices
- ▸Rapid development with functional paradigms
Notable Features
- ▸Real-time communication via Channels
- ▸Ecto for database access and migrations
- ▸MVC architecture with functional patterns
- ▸Hot code reloading for rapid development
- ▸PubSub system for scalable messaging
Origin & Creator
Created by Chris McCord and released in 2014, maintained by the Phoenix Core Team and Elixir community.
Industrial Note
Phoenix is widely used for high-concurrency web applications, real-time dashboards, APIs, chat systems, and fault-tolerant systems where low-latency and scalability are critical.
Quick Explain
- ▸Phoenix uses Elixir and the BEAM VM to provide fault-tolerant, concurrent, and low-latency web applications.
- ▸It follows the MVC architecture, with emphasis on functional programming and immutable data.
- ▸Built-in support for real-time features via Channels and PubSub.
- ▸Provides generators, Ecto for database interactions, and a templating system for views.
- ▸Hot code reloading and lightweight processes allow rapid development and highly concurrent systems.
Core Features
- ▸Controllers, models, and views with MVC pattern
- ▸Ecto ORM and query DSL
- ▸Templating system (HEEx)
- ▸WebSockets and real-time channels
- ▸Functional programming with immutable data
Learning Path
- ▸Week 1: Elixir basics and functional programming
- ▸Week 2: MVC pattern and Phoenix basics
- ▸Week 3: Ecto, schemas, and migrations
- ▸Week 4: Channels and real-time features
- ▸Week 5: Testing, deployment, and performance optimization
Practical Examples
- ▸Build a real-time chat application with Channels
- ▸Create a RESTful API backend with Ecto
- ▸Develop live dashboards using LiveView
- ▸Implement role-based authentication and authorization
- ▸Integrate third-party APIs for notifications or payments
Comparisons
- ▸Phoenix vs Rails -> Phoenix is highly concurrent and functional; Rails is convention-heavy and synchronous
- ▸Phoenix vs Django -> Phoenix uses Elixir and BEAM concurrency; Django is Python-based and synchronous
- ▸Phoenix vs Laravel -> Phoenix is functional and real-time capable; Laravel is PHP-based with MVC
- ▸Phoenix vs Node.js frameworks -> Phoenix offers BEAM fault-tolerance and lightweight processes; Node.js single-threaded event loop
- ▸Phoenix vs Play -> Both JVM/BEAM frameworks; Phoenix is functional and uses Elixir, Play is OOP with Java/Scala
Strengths
- ▸High concurrency using lightweight processes
- ▸Fault-tolerant and highly reliable due to BEAM VM
- ▸Real-time communication built-in
- ▸Hot code reloading for development speed
- ▸Functional, maintainable, and testable codebase
Limitations
- ▸Smaller ecosystem compared to Rails or Django
- ▸Requires knowledge of Elixir and functional programming
- ▸Less mature libraries for some niche needs
- ▸Deployment on BEAM may require learning OTP conventions
- ▸Not ideal for very simple static sites
When NOT to Use
- ▸Small static websites
- ▸Projects where Elixir/functional knowledge is unavailable
- ▸Simple CRUD apps with no concurrency requirement
- ▸Teams unfamiliar with OTP principles
- ▸Projects needing extensive libraries outside Elixir ecosystem
Cheat Sheet
- ▸mix phx.new my_app -> create new project
- ▸mix phx.gen.html/contexts -> generate scaffolding
- ▸mix ecto.create/migrate -> database setup
- ▸mix phx.server -> run server
- ▸mix test -> run tests
FAQ
- ▸Is Phoenix open-source? -> Yes, MIT License
- ▸Does Phoenix support real-time apps? -> Yes, via Channels and PubSub
- ▸Can Phoenix handle high concurrency? -> Yes, using BEAM processes
- ▸Does Phoenix work with relational databases? -> Yes, via Ecto
- ▸Is Phoenix suitable for enterprise apps? -> Yes, for fault-tolerant, scalable systems
30-Day Skill Plan
- ▸Master Elixir functional paradigms
- ▸Build efficient Ecto queries and schemas
- ▸Use LiveView for interactive UIs
- ▸Implement PubSub for scalable messaging
- ▸Write comprehensive tests and CI/CD integration
Final Summary
- ▸Phoenix is a functional, high-performance web framework in Elixir.
- ▸Provides MVC, real-time Channels, Ecto ORM, and HEEx templates.
- ▸Designed for fault-tolerant, concurrent, and scalable applications.
- ▸Hot code reloading and generators improve developer productivity.
- ▸Ideal for APIs, real-time apps, dashboards, and distributed systems.
Project Structure
- ▸lib/ - application and web code
- ▸lib/my_app_web/ - controllers, views, channels
- ▸config/ - environment and database configuration
- ▸priv/ - static assets, templates, migrations
- ▸test/ - unit, controller, and integration tests
Monetization
- ▸Open-source (MIT License) framework
- ▸Consulting for high-performance Phoenix apps
- ▸Enterprise system development
- ▸LiveView dashboards for SaaS platforms
- ▸Training and workshops for Phoenix developers
Productivity Tips
- ▸Use generators for scaffolding
- ▸Leverage LiveView for dynamic UI
- ▸Reuse contexts for domain logic
- ▸Optimize queries and Channels
- ▸Automate tests and CI/CD pipelines
Basic Concepts
- ▸Router - maps requests to controllers
- ▸Controller - handles request logic
- ▸View - renders templates (HEEx)
- ▸Template - HTML rendering system
- ▸Channel - real-time bi-directional communication
Official Docs
- ▸https://hexdocs.pm/phoenix
- ▸Phoenix GitHub repository
- ▸Elixir Forum and community guides