diff --git a/phoenix-migrations.md b/phoenix-migrations.md index 801a65996..c726ee2c5 100644 --- a/phoenix-migrations.md +++ b/phoenix-migrations.md @@ -6,14 +6,20 @@ category: Elixir ## Creating ``` -$ mix ecto.gen.migration add_posts_table - creating priv/repo/migrations/20160602085927_add_posts_table.exs +$ mix ecto.gen.migration update_posts_table + creating priv/repo/migrations/20160602085927_update_posts_table.exs ... $ mix ecto.migrate $ mix ecto.rollback ``` +### Creating models + +``` +$ mix phoenix.gen.model Message messages user_id:integer content:text +# creates models and tests +``` ## Tables diff --git a/phoenix-routing.md b/phoenix-routing.md new file mode 100644 index 000000000..5c0a8581c --- /dev/null +++ b/phoenix-routing.md @@ -0,0 +1,73 @@ +--- +title: "Phoenix: Routing" +category: Elixir +--- + +```sh +mix phoenix.routes +``` + +## Single routes + +```elixir +get "/", PageController, :index +``` + +## Resources + +```elixir +resources "/users", UserController +resources "/users", UserController, only: [:index, :show] +resources "/users", UserController, except: [:delete] +``` + +| Method | Path | Helper | +| ---- | ---- | ---- | +| GET | `/users` | `user_path(:index)` | +| GET | `/users/new` | `user_path(:new)` | +| GET | `/users/:id` | `user_path(:show, user)` | +| GET | `/users/:id/edit` | `user_path(:edit, user)` | +| POST | `/users` | `user_path(:create, user)` | +| PATCH/PUT | `/users/:id` | `user_path(:update, user)` | +| DELETE | `/users/:id` | `user_path(:delete, user)` | + +## Path helpers + +```elixir +user_path(Endpoint, :index) #=> /users +user_path(Endpoint, :show, 17) #=> /users/17 +user_path(Endpoint, :show, %User{id: 17}) #=> /users/17 +user_path(Endpoint, :show, 17, admin: true) #=> /users/17?admin=true + +user_path(Endpoint, :index) #=> "http://localhost:4000/users" +``` + +```elixir +MyApp.Router.Helpers.user_path(MyApp.Endpoint, :index) +``` + +## Nested resources + +```elixir +resources "/users", UserController do + resources "/posts", PostController +end + +user_post_path(:index, 17) #=> /users/17/posts +user_post_path(:show, 17, 12) #=> /users/17/posts/12 +``` + +## Scoped routes + +```elixir +scope "/admin" do + pipe_through :browser + resources "/reviews", MyApp.Admin.ReviewController +end +# reviews_path() -> /admin/reviews +``` + +```elixir +scope "/admin", as: :admin do: ... end +# admin_reviews_path() -> /admin/reviews +``` diff --git a/phoenix.md b/phoenix.md index 9f1479af6..08f379669 100644 --- a/phoenix.md +++ b/phoenix.md @@ -16,24 +16,46 @@ web/ static/ ``` -## Migrations +## Plug.Conn -``` -$ mix ecto.gen.migration add_posts_table - creating priv/repo/migrations/20160602085927_add_posts_table.exs - ... +### Request -$ mix ecto.migrate +```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"}] ``` -### [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration) +### Response + +```elixir +conn.resp_body #=> "..." +conn.resp_charset #=> "utf-8" +conn.resp_cookies #=> ... +conn.resp_headers #=> ... +conn.status #=> ... +``` + +### Misc + +```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 +``` + +### Session ``` -create index(:posts, [:slug], concurrently: true) -create table(:documents) do - add :body, :string - add :deletion_key, :string - - timestamps -end +conn = put_session(conn, :message, "new stuff we just set in the session") +get_session(conn, :message) ```