Update more sheets
This commit is contained in:
parent
da2090ee1f
commit
9fb0bed286
|
@ -1,42 +1,55 @@
|
||||||
---
|
---
|
||||||
title: "Phoenix: Plug.Conn"
|
title: "Phoenix: Plug.Conn"
|
||||||
category: Elixir
|
category: Elixir
|
||||||
|
layout: 2017/sheet
|
||||||
|
updated: 201708.29
|
||||||
|
weight: -5
|
||||||
---
|
---
|
||||||
|
|
||||||
Request
|
Request
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
### Request
|
||||||
|
{: .-prime}
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
conn.host #=> "example.com"
|
||||||
|
conn.method #=> "GET"
|
||||||
|
conn.path_info #=> ["posts", "1"]
|
||||||
|
conn.request_path #=> "/posts/1"
|
||||||
|
conn.query_string #=> "utm_source=twitter"
|
||||||
|
conn.port #=> 80
|
||||||
|
conn.scheme #=> :http
|
||||||
|
conn.peer #=> { {127, 0, 0, 1}, 12345 }
|
||||||
|
conn.remote_ip #=> { 151, 236, 219, 228 }
|
||||||
|
conn.req_headers #=> [{"content-type", "text/plain"}]
|
||||||
|
```
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn.host #=> "example.com"
|
|
||||||
conn.method #=> "GET"
|
|
||||||
conn.path_info #=> ["posts", "1"]
|
|
||||||
conn.request_path #=> "/posts/1"
|
|
||||||
conn.query_string #=> "utm_source=twitter"
|
|
||||||
conn.port #=> 80
|
|
||||||
conn.scheme #=> :http
|
|
||||||
conn.peer #=> { {127, 0, 0, 1}, 12345 }
|
|
||||||
conn.remote_ip #=> { 151, 236, 219, 228 }
|
|
||||||
conn.req_headers #=> [{"content-type", "text/plain"}]
|
|
||||||
conn |> get_req_header("referrer")
|
conn |> get_req_header("referrer")
|
||||||
```
|
```
|
||||||
|
|
||||||
### Updating
|
### Updating conn
|
||||||
Usually only useful for tests.
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn
|
conn
|
||||||
|> put_req_header("accept", "application/json")
|
|> put_req_header("accept", "application/json")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Usually only useful for tests.
|
||||||
|
|
||||||
Response
|
Response
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
### Response
|
||||||
|
{: .-prime}
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn.resp_body #=> "..."
|
conn.resp_body #=> "..."
|
||||||
conn.resp_charset #=> "utf-8"
|
conn.resp_charset #=> "utf-8"
|
||||||
conn.resp_cookies #=> ...
|
conn.resp_cookies #=> ...
|
||||||
conn.resp_headers #=> ...
|
conn.resp_headers #=> ...
|
||||||
conn.status #=> ...
|
conn.status #=> ...
|
||||||
```
|
```
|
||||||
|
|
||||||
### Sending responses
|
### Sending responses
|
||||||
|
@ -47,19 +60,27 @@ conn
|
||||||
|> html("<html><head>...")
|
|> html("<html><head>...")
|
||||||
|> json(%{ message: "Hello" })
|
|> json(%{ message: "Hello" })
|
||||||
|> text("Hello")
|
|> text("Hello")
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> redirect(to: "/foo")
|
|> redirect(to: "/foo")
|
||||||
|> redirect(external: "http://www.google.com/")
|
|> redirect(external: "http://www.google.com/")
|
||||||
|> halt()
|
|> halt()
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> put_resp_content_type("text/plain")
|
|> put_resp_content_type("text/plain")
|
||||||
|> put_resp_cookie("abc", "def")
|
|> put_resp_cookie("abc", "def")
|
||||||
|> put_resp_header("X-Delivered-By", "myapp")
|
|> put_resp_header("X-Delivered-By", "myapp")
|
||||||
|> put_status(202)
|
|> put_status(202)
|
||||||
|> put_status(:not_found)
|
|> put_status(:not_found)
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> put_private(:plug_foo, "...") # reserved for libraries
|
|> put_private(:plug_foo, "...") # reserved for libraries
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> send_resp(201, "")
|
|> send_resp(201, "")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -71,14 +92,18 @@ conn
|
||||||
|> render("index.html")
|
|> render("index.html")
|
||||||
|> render("index.html", hello: "world")
|
|> render("index.html", hello: "world")
|
||||||
|> render(MyApp.ErrorView, "404.html")
|
|> render(MyApp.ErrorView, "404.html")
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> put_layout(:foo)
|
|> put_layout(:foo)
|
||||||
|> put_layout(false)
|
|> put_layout(false)
|
||||||
|> put_view(ErrorView)
|
|> put_view(ErrorView)
|
||||||
|> put_secure_browser_headers()
|
|> put_secure_browser_headers()
|
||||||
# prevent clickjacking, nosniff, and xss protection
|
# prevent clickjacking, nosniff, and xss protection
|
||||||
# x-frame-options, x-content-type-options, x-xss-protection
|
# x-frame-options, x-content-type-options, x-xss-protection
|
||||||
|
```
|
||||||
|
|
||||||
|
```elixir
|
||||||
|> put_new_view(ErrorView) # if not set yet
|
|> put_new_view(ErrorView) # if not set yet
|
||||||
|> put_new_layout(:foo)
|
|> put_new_layout(:foo)
|
||||||
```
|
```
|
||||||
|
@ -87,8 +112,21 @@ conn
|
||||||
layout(conn)
|
layout(conn)
|
||||||
```
|
```
|
||||||
|
|
||||||
Accepts
|
Other features
|
||||||
-------
|
--------------
|
||||||
|
|
||||||
|
### Other fields
|
||||||
|
{: .-prime}
|
||||||
|
|
||||||
|
```elixir
|
||||||
|
conn.assigns # storage of crap
|
||||||
|
conn.owner # process
|
||||||
|
conn.halted # if pipeline was halted
|
||||||
|
conn.secret_key_base # ...
|
||||||
|
conn.state # :unset, :set, :file, :sent, :chunked
|
||||||
|
```
|
||||||
|
|
||||||
|
### Accepts
|
||||||
|
|
||||||
```js
|
```js
|
||||||
plug :accepts, ["html", "json"]
|
plug :accepts, ["html", "json"]
|
||||||
|
@ -97,17 +135,7 @@ get_format(conn) #=> "html"
|
||||||
conn.accepts
|
conn.accepts
|
||||||
```
|
```
|
||||||
|
|
||||||
## Misc
|
### Assigns
|
||||||
|
|
||||||
```elixir
|
|
||||||
conn.assigns # storage of crap
|
|
||||||
conn.owner # process
|
|
||||||
conn.halted # if pipeline was halted
|
|
||||||
conn.secret_key_base # ...
|
|
||||||
conn.state # :unset, :set, :file, :sent, :chunked
|
|
||||||
```
|
|
||||||
|
|
||||||
## Assigns
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn.assigns[:hello]
|
conn.assigns[:hello]
|
||||||
|
@ -119,7 +147,7 @@ conn = async_assign(conn, :location, fn -> geoip_lookup() end)
|
||||||
await_assign(conn, :location)
|
await_assign(conn, :location)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Session
|
### Session
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn = fetch_session(conn) # or plug :fetch_session
|
conn = fetch_session(conn) # or plug :fetch_session
|
||||||
|
@ -129,13 +157,11 @@ get_session(conn, :message)
|
||||||
conn = clear_session(conn)
|
conn = clear_session(conn)
|
||||||
```
|
```
|
||||||
|
|
||||||
Also: `flash` `cookie` `params`
|
|
||||||
|
|
||||||
```elixir
|
```elixir
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Success")
|
|> put_flash(:info, "Success")
|
||||||
|> put_flash(:error, "Oh no")
|
|> put_flash(:error, "Oh no")
|
||||||
```
|
```
|
||||||
|
|
||||||
```elixir
|
Also available: `flash` `cookie` `params`
|
||||||
```
|
|
||||||
|
|
|
@ -2,4 +2,5 @@
|
||||||
title: "Phoenix: Ecto models"
|
title: "Phoenix: Ecto models"
|
||||||
category: Elixir
|
category: Elixir
|
||||||
redirect_to: /phoenix-ecto
|
redirect_to: /phoenix-ecto
|
||||||
|
deprecated: true
|
||||||
---
|
---
|
||||||
|
|
Loading…
Reference in New Issue