OSDN Git Service

Regular updates
[twpd/master.git] / phoenix.md
1 ---
2 title: Phoenix
3 category: Elixir
4 layout: 2017/sheet
5 weight: -1
6 updated: 2018-03-06
7 ---
8
9 ### Quick start
10
11 ```bash
12 # Install Phoenix
13 mix local.hex
14 mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
15 ```
16
17 ```bash
18 # Create a new project
19 mix phx.new hello
20 ```
21
22 ```bash
23 # Start the application
24 mix phx.server
25 ```
26
27 Install Erlang, Elixir, Node.js, PostgreSQL first.
28 See: [Installation](https://hexdocs.pm/phoenix/installation.html) _(hexdocs.pm)_
29
30 ### Directory structure
31
32 ```
33 ./
34 ├── _build
35 ├── assets/
36 │   ├── css/
37 │   ├── js/
38 │   ├── static/
39 │   └── node_modules/
40 ├── config/
41 ├── deps/
42 ├── lib/
43 │   ├── hello/
44 │   ├── hello.ex
45 │   ├── hello_web/
46 │   │   ├── channels/
47 │   │   ├── controllers/
48 │   │   ├── templates/
49 │   │   ├── views/
50 │   │   ├── router.ex
51 │   │   └── gettext.ex
52 │   └── hello_web.ex
53 ├── priv/
54 └── test/
55 ```
56 {: .-box-chars}
57
58 See: [Adding pages](https://hexdocs.pm/phoenix/adding_pages.html) _(hexdocs.pm)_
59
60 ### Migrations
61
62 ```bash
63 $ mix ecto.gen.migration update_posts_table
64   creating priv/repo/migrations/20160602085927_update_posts_table.exs
65   ···
66 ```
67
68 ```elixir
69 create table(:documents) do
70   add :title, :string
71   add :title, :string, default: "Hello"
72   add :body, :text
73   add :age, :integer
74   add :price, :float, precision: 10, scale: 2
75   timestamps
76 end
77 ```
78
79 [Ecto migrations cheatsheet](./phoenix-migrations)
80 {: .-crosslink}
81
82 ### Routing
83
84 ```elixir
85 get "/", PageController, :index
86
87 resources "/users", UserController do
88   resources "/posts", PostController
89 end
90 ```
91
92 ```elixir
93 user_post_path(conn, :index, 17)     # → /users/17/posts
94 user_post_path(conn, :show, 17, 12)  # → /users/17/posts/12
95 ```
96
97 [Phoenix routing cheatsheet](./phoenix-routing)
98 {: .-crosslink}
99
100 ### Conn
101
102 ```elixir
103 conn.host          # → "example.com"
104 conn.method        # → "GET"
105 conn.path_info     # → ["posts", "1"]
106 conn.request_path  # → "/posts/1"
107 ```
108
109 ```elixir
110 conn
111 |> put_status(202)
112 |> html("<html><head>···")
113 |> json(%{ message: "Hello" })
114 |> text("Hello")
115 |> redirect(to: "/foo")
116 |> render("index.html")
117 |> render("index.html", hello: "world")
118 |> render(MyApp.ErrorView, "404.html")
119 ```
120
121 [Phoenix conn cheatsheet](./phoenix-conn)
122 {: .-crosslink}
123
124 ### Ecto
125
126 ```bash
127 $ mix phx.gen.html \
128     Accounts \       # domain
129     Profile \        # schema
130     profiles \       # table name
131     email:string \
132     age:integer
133 ```
134
135 [Ecto cheatsheet](./phoenix-ecto)
136 {: .-crosslink}
137
138 ### Also see
139
140 - [Phoenix framework site](http://phoenixframework.org/) _(phoenixframework.org)_
141 - [Phoenix: getting started](https://hexdocs.pm/phoenix/overview.html) _(hexdocs.pm)_