Peace of mind from prototype to production - comprehensive web framework for Elixir
pkg:hex/phoenix@1.8.x
npx @tessl/cli install tessl/hex-phoenix@1.8.0Phoenix is a comprehensive web framework for Elixir that provides a complete foundation for building scalable, maintainable web applications and APIs. It offers productive development through live code reloading, comprehensive generators, real-time features via channels and LiveView, robust authentication systems, and built-in security measures leveraging the fault-tolerant Erlang VM.
{:phoenix, "~> 1.8"} in mix.exs# In your application
defmodule MyAppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :my_app
end
# In controllers
defmodule MyAppWeb.PageController do
use Phoenix.Controller, formats: [:html, :json]
end
# In channels
defmodule MyAppWeb.UserChannel do
use Phoenix.Channel
end
# Common imports for utilities
import Phoenix.Controller
import Phoenix.Token
import Phoenix.VerifiedRoutes# Define an endpoint
defmodule MyAppWeb.Endpoint do
use Phoenix.Endpoint, otp_app: :my_app
plug Plug.RequestId
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
plug MyAppWeb.Router
end
# Define routes
defmodule MyAppWeb.Router do
use Phoenix.Router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :protect_from_forgery
end
scope "/", MyAppWeb do
pipe_through :browser
get "/", PageController, :index
resources "/users", UserController
end
end
# Handle requests
defmodule MyAppWeb.PageController do
use Phoenix.Controller, formats: [:html]
def index(conn, _params) do
render(conn, "index.html", title: "Welcome")
end
endPhoenix follows the Model-View-Controller pattern with additional layers:
Application-level configuration and JSON library management.
defmodule Phoenix do
def json_library() :: module
def plug_init_mode() :: :compile | :runtime
endCore web server functionality, request routing, and response handling.
defmodule Phoenix.Endpoint do
@callback start_link(keyword) :: Supervisor.on_start()
@callback config(atom, term) :: term
@callback url() :: String.t()
@callback struct_url() :: URI.t()
@callback static_path(String.t()) :: String.t()
@callback host() :: String.t()
@callback server_info(Plug.Conn.scheme()) :: {:ok, {String.t(), pos_integer}} | :error
@callback subscribe(binary, keyword) :: :ok | {:error, term}
@callback unsubscribe(binary) :: :ok | {:error, term}
@callback broadcast(binary, binary, term) :: :ok | {:error, term}
@callback broadcast!(binary, binary, term) :: :ok
end
defmodule Phoenix.Router do
defmacro get(path, plug, plug_opts \\ [], options \\ [])
defmacro post(path, plug, plug_opts \\ [], options \\ [])
defmacro put(path, plug, plug_opts \\ [], options \\ [])
defmacro patch(path, plug, plug_opts \\ [], options \\ [])
defmacro delete(path, plug, plug_opts \\ [], options \\ [])
defmacro resources(path, controller, opts \\ [])
defmacro scope(options \\ [], do: context)
defmacro pipeline(name, do: block)
defmacro plug(plug, opts \\ [])
defmacro pipe_through(pipes)
end
defmodule Phoenix.Controller do
def action_name(conn) :: atom
def controller_module(conn) :: atom
def render(conn, template_or_assigns \\ [])
def json(conn, data)
def text(conn, data)
def html(conn, data)
def redirect(conn, opts)
def put_view(conn, formats)
def put_layout(conn, layout)
endWeb Foundation and Request Handling
WebSocket channels, broadcasting, and bidirectional communication.
defmodule Phoenix.Channel do
@callback join(binary, map, Phoenix.Socket.t()) ::
{:ok, Phoenix.Socket.t()} | {:error, term}
@callback handle_in(binary, map, Phoenix.Socket.t()) ::
{:noreply, Phoenix.Socket.t()} | {:reply, map, Phoenix.Socket.t()}
def broadcast(socket, event, message)
def push(socket, event, message)
end
defmodule Phoenix.Socket do
@callback connect(map, Phoenix.Socket.t(), map) ::
{:ok, Phoenix.Socket.t()} | :error
endToken generation, CSRF protection, and secure headers.
defmodule Phoenix.Token do
def sign(context, salt, data, opts \\ [])
def verify(context, salt, token, opts \\ [])
end
defmodule Phoenix.Controller do
def protect_from_forgery(conn, opts \\ [])
def put_secure_browser_headers(conn, headers \\ %{})
endMix tasks for generating boilerplate code and development utilities.
# Mix tasks (command line)
mix phx.gen.html Accounts User users name:string email:string
mix phx.gen.json Accounts User users name:string email:string
mix phx.gen.live Accounts User users name:string email:string
mix phx.gen.auth Accounts User users
mix phx.gen.channel RoomChannelCode Generation and Development Tools
Test helpers for controllers, channels, and integration testing.
defmodule Phoenix.ConnTest do
def build_conn(method \\ :get, path \\ "/", params_or_body \\ nil)
def get(conn, path, params \\ nil)
def post(conn, path, params \\ nil)
def json_response(conn, status)
end
defmodule Phoenix.ChannelTest do
def connect(handler, params, connect_info \\ %{})
def subscribe_and_join(socket, channel, topic, payload \\ %{})
def push(socket, event, payload \\ %{})
endTrack user presence across distributed nodes with real-time updates.
defmodule Phoenix.Presence do
@callback track(Phoenix.Socket.t(), binary, map) :: {:ok, binary}
@callback list(Phoenix.Socket.t() | binary) :: map
def track(socket, key, meta)
def untrack(socket, key)
def update(socket, key, meta_or_fun)
end# Core socket type for real-time communication
defmodule Phoenix.Socket do
defstruct [
:id, :assigns, :channel, :channel_pid, :endpoint,
:handler, :joined, :join_ref, :ref, :pubsub_server,
:topic, :transport, :transport_pid, :serializer
]
@type t :: %__MODULE__{}
end
# Connection type for HTTP requests
@type Plug.Conn.t :: %Plug.Conn{}
# Token options
@type token_opts :: [
max_age: pos_integer,
key_iterations: pos_integer,
key_length: pos_integer,
key_digest: atom
]