cheatsheets/phoenix.md

141 lines
2.7 KiB
Markdown
Raw Permalink Normal View History

2016-06-02 09:39:31 +00:00
---
title: Phoenix
category: Elixir
2017-09-04 03:08:40 +00:00
weight: -1
2020-07-04 13:33:09 +00:00
updated: 2018-03-06
2016-06-02 09:39:31 +00:00
---
2017-09-04 03:25:54 +00:00
### Quick start
```bash
# Install Phoenix
mix local.hex
mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
```
```bash
# Create a new project
mix phx.new hello
```
```bash
# Start the application
mix phx.server
```
2017-09-04 06:19:43 +00:00
Install Erlang, Elixir, Node.js, PostgreSQL first.
2017-09-04 03:25:54 +00:00
See: [Installation](https://hexdocs.pm/phoenix/installation.html) _(hexdocs.pm)_
2017-09-04 03:08:40 +00:00
### Directory structure
2016-06-02 09:39:31 +00:00
```
2017-09-04 03:20:35 +00:00
./
├── _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
2017-09-04 03:20:35 +00:00
├── priv/
└── test/
2016-06-02 09:39:31 +00:00
```
2017-09-04 03:20:35 +00:00
{: .-box-chars}
2016-06-02 09:39:31 +00:00
2017-09-04 03:20:35 +00:00
See: [Adding pages](https://hexdocs.pm/phoenix/adding_pages.html) _(hexdocs.pm)_
2017-09-04 03:08:40 +00:00
### Migrations
```bash
$ mix ecto.gen.migration update_posts_table
creating priv/repo/migrations/20160602085927_update_posts_table.exs
2017-09-04 03:39:31 +00:00
···
2017-09-04 03:08:40 +00:00
```
```elixir
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
```
2017-09-04 06:19:43 +00:00
[Ecto migrations cheatsheet](./phoenix-migrations)
2017-09-04 03:38:12 +00:00
{: .-crosslink}
2017-09-04 03:08:40 +00:00
2017-09-04 06:19:43 +00:00
### Routing
2017-09-04 03:08:40 +00:00
```elixir
get "/", PageController, :index
resources "/users", UserController do
resources "/posts", PostController
end
```
```elixir
user_post_path(conn, :index, 17) # → /users/17/posts
user_post_path(conn, :show, 17, 12) # → /users/17/posts/12
2017-09-04 03:08:40 +00:00
```
2017-09-04 06:19:43 +00:00
[Phoenix routing cheatsheet](./phoenix-routing)
2017-09-04 03:38:12 +00:00
{: .-crosslink}
2017-09-04 03:08:40 +00:00
2017-09-04 06:19:43 +00:00
### Conn
2017-09-04 03:08:40 +00:00
```elixir
2017-09-04 03:40:17 +00:00
conn.host # → "example.com"
conn.method # → "GET"
conn.path_info # → ["posts", "1"]
conn.request_path # → "/posts/1"
2017-09-04 03:08:40 +00:00
```
```elixir
conn
|> put_status(202)
2017-09-04 03:39:31 +00:00
|> html("<html><head>···")
2017-09-04 03:08:40 +00:00
|> json(%{ message: "Hello" })
|> text("Hello")
|> redirect(to: "/foo")
|> render("index.html")
|> render("index.html", hello: "world")
|> render(MyApp.ErrorView, "404.html")
```
2017-09-04 06:19:43 +00:00
[Phoenix conn cheatsheet](./phoenix-conn)
2017-09-04 03:38:12 +00:00
{: .-crosslink}
2017-09-04 03:08:40 +00:00
2017-09-04 06:19:43 +00:00
### Ecto
2017-09-04 03:08:40 +00:00
```bash
$ mix phx.gen.html \
Accounts \ # domain
Profile \ # schema
profiles \ # table name
email:string \
age:integer
```
2017-09-04 06:19:43 +00:00
[Ecto cheatsheet](./phoenix-ecto)
{: .-crosslink}
2017-09-04 03:08:40 +00:00
### Also see
2017-09-04 03:20:35 +00:00
- [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_
- [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_