Catalog / Phoenix Framework Cheatsheet
Phoenix Framework Cheatsheet
A comprehensive cheat sheet covering essential Phoenix framework concepts, commands, and code snippets for efficient web development in Elixir.
Project Setup & Core Concepts
Project Generation
Creating a new Phoenix project:
This command generates a new Phoenix project named |
Creating a new Phoenix project with
Use the |
Starting the Phoenix server:
Starts the Phoenix application server. By default, it runs on port 4000. |
Key Directories
|
Contains the core web application code, including controllers, views, channels, and templates. |
|
Contains the application’s business logic and domain models. |
|
Contains database migrations and schema definitions (if using Ecto). |
|
Contains configuration files for different environments (dev, test, prod). |
Core Components
Router |
Directs incoming HTTP requests to the appropriate controller action. |
Controller |
Handles user requests, interacts with models, and renders views. |
View |
Prepares data for presentation in templates. |
Template |
Generates the HTML output using data provided by the view (typically |
Channel |
Handles real-time communication using WebSockets. |
Routing and Controllers
Defining Routes
Routes are defined in
|
|
|
Controller Actions
A controller action receives
|
|
|
|
Working with Parameters
Accessing parameters |
Parameters are available in the
|
Strong parameters |
Use
|
Ecto Integration
Defining Schemas
Schemas define the structure of your database tables. They reside in
|
|
|
|
|
Changesets
Changesets are used to validate and cast data before saving it to the database.
|
|
|
|
|
Repo Operations
Inserting data |
|
Updating data |
|
Deleting data |
|
Retrieving data |
|
Templates and Views
Template Syntax
Phoenix templates use the
Use
|
|
|
View Modules
Views prepare data for templates. They are defined in
|
|
Define helper functions to format data or perform other logic for your templates. |
Layouts
Default Layout |
The main layout is in |
Rendering Content |
Use |
Custom Layouts |
You can specify a different layout in your controller using
|