Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
142 lines (115 sloc) 2.71 KB
title category layout weight updated
Phoenix
Elixir
2017/sheet
-1
2018-01-15

Quick start

# Install Phoenix
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
# Create a new project
mix phx.new hello
# Start the application
mix phx.server

Install Erlang, Elixir, Node.js, PostgreSQL first. See: Installation (hexdocs.pm)

Directory structure

./
├── _build
├── assets/
│   ├── css/
│   ├── js/
│   ├── static/
│   └── node_modules/
├── config/
├── deps/
├── lib/
│   ├── hello/
│   ├── hello.ex
│   ├── hello_web/
│   │   ├── channels/
│   │   ├── controllers/
│   │   ├── templates/
│   │   ├── views/
│   │   ├── router.ex
│   │   └── gettext.ex
│   └── hello_web.ex
├── priv/
└── test/

{: .-box-chars}

See: Adding pages (hexdocs.pm)

Migrations

$ mix ecto.gen.migration update_posts_table
  creating priv/repo/migrations/20160602085927_update_posts_table.exs
  ···
create table(:documents) do
  add :title, :string
  add :title, :string, default: "Hello"
  add :body, :text
  add :age, :integer
  add :price, :float, precision: 10, scale: 2
  timestamps
end

Ecto migrations cheatsheet {: .-crosslink}

Routing

get "/", PageController, :index

resources "/users", UserController do
  resources "/posts", PostController
end
user_post_path(conn, :index, 17)     # → /users/17/posts
user_post_path(conn, :show, 17, 12)  # → /users/17/posts/12

Phoenix routing cheatsheet {: .-crosslink}

Conn

conn.host          # → "example.com"
conn.method        # → "GET"
conn.path_info     # → ["posts", "1"]
conn.request_path  # → "/posts/1"
conn
|> put_status(202)
|> html("<html><head>···")
|> json(%{ message: "Hello" })
|> text("Hello")
|> redirect(to: "/foo")
|> render("index.html")
|> render("index.html", hello: "world")
|> render(MyApp.ErrorView, "404.html")

Phoenix conn cheatsheet {: .-crosslink}

Ecto

$ mix phx.gen.html \
    Accounts \       # domain
    Profile \        # schema
    profiles \       # table name
    email:string \
    age:integer

Ecto cheatsheet {: .-crosslink}

Also see

You can’t perform that action at this time.