### Next steps when skipping dependency installation Source: https://phoenix.hexdocs.pm/up_and_running.md Shows the output when the user answers n to installing dependencies, prompting to cd into the project and run mix deps.get, then configure the database and start the server. ```console Fetch and install dependencies? [Yn] n We are almost there! The following steps are missing: $ cd hello $ mix deps.get Then configure your database in config/dev.exs and run: $ mix ecto.create Start your Phoenix app with: $ mix phx.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phx.server ``` -------------------------------- ### Install dependencies and next steps after mix phx.new Source: https://phoenix.hexdocs.pm/up_and_running.md Shows the output when the user answers Y to install dependencies, including running mix deps.get, mix assets.setup, and mix deps.compile, followed by the steps to configure the database, create it, and start the server. ```console Fetch and install dependencies? [Yn] Y * running mix deps.get * running mix assets.setup * running mix deps.compile We are almost there! The following steps are missing: $ cd hello Then configure your database in config/dev.exs and run: $ mix ecto.create Start your Phoenix app with: $ mix phx.server You can also run your app inside IEx (Interactive Elixir) as: $ iex -S mix phx.server ``` -------------------------------- ### Phoenix.Endpoint.server?/2 example Source: https://phoenix.hexdocs.pm/Phoenix.Endpoint.html Shows how to check if an endpoint's web server is configured to start. Returns true when the server is configured. ```elixir iex> Phoenix.Endpoint.server?(:my_app, MyAppWeb.Endpoint) true ``` -------------------------------- ### PostControllerTest setup and index test Source: https://phoenix.hexdocs.pm/testing_controllers.html Example of the generated controller test file, showing the use of ConnCase, fixtures, module attributes, and a describe block for the index action. ```elixir defmodule HelloWeb.PostControllerTest do use HelloWeb.ConnCase import Hello.BlogFixtures @create_attrs %{body: "some body", title: "some title"} @update_attrs %{body: "some updated body", title: "some updated title"} @invalid_attrs %{body: nil, title: nil} describe "index" do test "lists all posts", %{conn: conn} do conn = get(conn, ~p"/posts") assert html_response(conn, 200) =~ "Listing Posts" end end ... ``` -------------------------------- ### Start Phoenix server with mix phx.server Source: https://phoenix.hexdocs.pm/json_and_apis.html Starts the Phoenix server for local development. ```shell $ mix phx.server ``` -------------------------------- ### put_view example Source: https://phoenix.hexdocs.pm/Phoenix.Controller.md Example call to put_view/2 with a keyword list mapping formats to view modules. ```elixir iex> put_view(conn, html: AppHTML, json: AppJSON) ``` -------------------------------- ### Router with API routes (lib/hello_web/router.ex) Source: https://phoenix.hexdocs.pm/json_and_apis.html Defines a Phoenix router with an API pipeline that accepts JSON and three routes: GET /, GET /:name, and POST /. This is the minimal setup for the examples. ```elixir defmodule HelloWeb.Router do use HelloWeb, :router pipeline :api do plug :accepts, ["json"] end scope "/api", HelloWeb do pipe_through :api get "/", HelloController, :show get "/:name", HelloController, :show post "/", HelloController, :show end end ``` -------------------------------- ### Curl requests for minimal example Source: https://phoenix.hexdocs.pm/json_and_apis.html Three curl commands that demonstrate how the minimal setup handles GET with path parameter, GET with query parameter, and POST with JSON body, all returning "Hello world!". ```bash $ curl http://localhost:4000/api/world $ curl http://localhost:4000/api?name=world $ curl -iX POST http://localhost:4000/api \ -H 'Content-Type: application/json' \ -d '{"name": "world"}' ``` -------------------------------- ### Example: bypass_through with router and GET request Source: https://phoenix.hexdocs.pm/Phoenix.ConnTest.html Example showing how to use bypass_through/2 with a router and a GET request to test an authentication plug in isolation. ```elixir conn = conn |> bypass_through(MyAppWeb.Router) |> get("/some_url") |> MyApp.RequireAuthentication.call([]) assert conn.halted ``` -------------------------------- ### mix phx.new hello_world example Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.New.md Creates a new Phoenix project named hello_world. Equivalent to specifying --module HelloWorld. ```shell $ mix phx.new hello_world ``` ```shell $ mix phx.new hello_world --module HelloWorld ``` -------------------------------- ### scope/3 example with host option Source: https://phoenix.hexdocs.pm/Phoenix.Router.html Example of defining a scope with path and host options, containing a GET route. ```elixir scope "/v1", host: "api." do get "/pages/:id", PageController, :show end ``` -------------------------------- ### Example: mix phx.new.ecto hello_ecto Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.New.Ecto.html Creates a new Ecto project named hello_ecto. This is equivalent to specifying --module HelloEcto. ```shell $ mix phx.new.ecto hello_ecto ``` -------------------------------- ### scope/4 example with alias and host Source: https://phoenix.hexdocs.pm/Phoenix.Router.html Example of defining a scope with path, alias, and host options, containing a GET route. ```elixir scope "/v1", API.V1, host: "api." do get "/pages/:id", PageController, :show end ``` -------------------------------- ### Provision a free database with gigalixir pg:create --free Source: https://phoenix.hexdocs.pm/gigalixir.md Creates a free tier database for your app. Run this command to provision a database at no cost. ```console $ gigalixir pg:create --free ``` -------------------------------- ### get/4 route example Source: https://phoenix.hexdocs.pm/Phoenix.Router.html Generates a route to handle a GET request to the given path. The example shows a typical route definition. ```elixir get("/events/:id", EventController, :action) ``` -------------------------------- ### Example product data after form submission Source: https://phoenix.hexdocs.pm/your_first_context.html Example output shown after submitting the product form with valid data, displaying the created product details. ```text Product created successfully. Title: Metaprogramming Elixir Description: Write Less Code, Get More Done (and Have Fun!) Price: 15.000000 Views: 0 ``` -------------------------------- ### Install Phoenix application generator with mix archive.install Source: https://phoenix.hexdocs.pm/installation.html Installs the phx_new archive, which provides the phx.new generator for creating new Phoenix applications. After installation, the generator is available for use in the next guide. ```shell $ mix archive.install hex phx_new ``` -------------------------------- ### Change into project directory Source: https://phoenix.hexdocs.pm/up_and_running.md Changes into the newly created hello directory before creating the database and starting the server. ```console $ cd hello ``` -------------------------------- ### sigil_p macro examples Source: https://phoenix.hexdocs.pm/Phoenix.VerifiedRoutes.md Examples of using the ~p sigil with route verification. Shows setup with use Phoenix.VerifiedRoutes, redirect usage, and HEEx link components with interpolated values. ```elixir use Phoenix.VerifiedRoutes, endpoint: MyAppWeb.Endpoint, router: MyAppWeb.Router redirect(to: ~p"/users/top") redirect(to: ~p"/users/#{@user}") ~H""" <.link href={~p"/users?page=#{@page}"}>profile <.link href={~p"/users?#{@params}"}>profile """ ``` -------------------------------- ### mix phx.new with explicit module name Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.New.html Equivalent to the previous example, but explicitly sets the module name to HelloWorld. ```shell $ mix phx.new hello_world --module HelloWorld ``` -------------------------------- ### Controller with show action (lib/hello_web/controllers/hello_controller.ex) Source: https://phoenix.hexdocs.pm/json_and_apis.html Defines a controller with a single `show` action that receives `conn` and `params` and renders the `:greet` template with the given params. ```elixir defmodule HelloWeb.HelloController do use HelloWeb, :controller def show(conn, params) do render(conn, :greet, params) end end ``` -------------------------------- ### Route definition: get "/users/:id", UserController, :show Source: https://phoenix.hexdocs.pm/Phoenix.Controller.md Example route that maps a GET request to the show action of UserController. This route causes the controller to be invoked as a plug. ```elixir get "/users/:id", UserController, :show ``` -------------------------------- ### mix phx.gen.html with --context-app option Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.Gen.Html.html Example command using the --context-app option to specify the context application. ```shell $ mix phx.gen.html Accounts User users --context-app my_app ``` -------------------------------- ### Example product details output Source: https://phoenix.hexdocs.pm/in_context_relationships.html Shows the expected output after saving a product with categories, including the categories list. ```text Title: Elixir Flashcards Description: Flash card set for the Elixir programming language Price: 5.000000 Views: 0 Categories: Education Books ``` -------------------------------- ### HTTP response for GET /api/urls Source: https://phoenix.hexdocs.pm/json_and_apis.html Example response showing a 200 status and empty JSON data array. ```http HTTP/1.1 200 OK cache-control: max-age=0, private, must-revalidate content-length: 11 content-type: application/json; charset=utf-8 date: Fri, 06 May 2022 21:22:42 GMT server: Cowboy x-request-id: Fuyg-wMl4S-hAfsAAAUk {"data":[]} ``` -------------------------------- ### Initialize Git repository Source: https://phoenix.hexdocs.pm/gigalixir.md Run these commands in the project directory to commit files to Git before deploying. ```console $ git init $ git add . $ git commit -m "Initial commit" ``` -------------------------------- ### mix phx.gen.html with --web flag Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.Gen.Html.html Example command using the --web flag to namespace the generated controller and HTML views. ```shell $ mix phx.gen.html Accounts User users --web Accounts ``` -------------------------------- ### Equivalent: mix phx.new.web hello_web --module HelloWeb Source: https://phoenix.hexdocs.pm/Mix.Tasks.Phx.New.Web.md Shows the explicit module option equivalent to the default behavior of the previous example. ```shell $ mix phx.new.web hello_web --module HelloWeb ``` -------------------------------- ### Update aliases in mix.exs Source: https://phoenix.hexdocs.pm/asset_management.md Modify the aliases task to install npm packages during mix setup and use the new esbuild for assets.deploy. ```elixir defp aliases do [ setup: ["deps.get", "ecto.setup", "cmd --cd assets npm install"], ..., "assets.deploy": ["cmd --cd assets node build.js --deploy", "phx.digest"] ] end ``` -------------------------------- ### Create database with mix ecto.create Source: https://phoenix.hexdocs.pm/swapping_databases.md Run mix ecto.create to create the database for the Repo. The output confirms the database has been created. ```console $ mix ecto.create ``` -------------------------------- ### static_integrity/2 examples Source: https://phoenix.hexdocs.pm/Phoenix.VerifiedRoutes.md Shows how to call static_integrity/2 with a conn, socket, or endpoint module to get the integrity hash for a static asset. The output is a hex string. ```elixir iex> static_integrity(conn, "/assets/js/app.js") "813dfe33b5c7f8388bccaaa38eec8382" ``` ```elixir iex> static_integrity(socket, "/assets/js/app.js") "813dfe33b5c7f8388bccaaa38eec8382" ``` ```elixir iex> static_integrity(AppWeb.Endpoint, "/assets/js/app.js") "813dfe33b5c7f8388bccaaa38eec8382" ``` -------------------------------- ### Channel join and message handling example Source: https://phoenix.hexdocs.pm/js/index.html This example shows how to join a channel, listen for 'new_msg' events, push messages, and handle join responses with ok/error/timeout matches. It uses a chat room scenario with a room token for authorization. ```javascript let channel = socket.channel("room:123", {token: roomToken}) channel.on("new_msg", msg => console.log("Got message", msg) ) $input.onEnter( e => { channel.push("new_msg", {body: e.target.val}, 10000) .receive("ok", (msg) => console.log("created message", msg) ) .receive("error", (reasons) => console.log("create failed", reasons) ) .receive("timeout", () => console.log("Networking issue...") ) }) channel.join() .receive("ok", ({messages}) => console.log("catching up", messages) ) .receive("error", ({reason}) => console.log("failed join", reason) ) .receive("timeout", () => console.log("Networking issue. Still waiting...")) ``` -------------------------------- ### Add redirect_test route in lib/hello_web/router.ex Source: https://phoenix.hexdocs.pm/controllers.html Adds a GET route for /redirect_test that maps to PageController's redirect_test action. This route is required for the redirect example to work. ```elixir defmodule HelloWeb.Router do ... scope "/", HelloWeb do ... get "/", PageController, :home get "/redirect_test", PageController, :redirect_test ... end end ``` -------------------------------- ### Connect to PostgreSQL and list tables Source: https://phoenix.hexdocs.pm/ecto.html Shows how to connect to the hello_dev database and list relations to verify the users table and its sequence exist. ```shell $ psql -U postgres Type "help" for help. postgres=# \connect hello_dev You are now connected to database "hello_dev" as user "postgres". hello_dev=# \d List of relations Schema | Name | Type | Owner --------+-------------------+----------+---------- public | schema_migrations | table | postgres public | users | table | postgres public | users_id_seq | sequence | postgres (3 rows) hello_dev=# \q ``` -------------------------------- ### Example product_params output with Plug.Upload Source: https://phoenix.hexdocs.pm/file_uploads.md Example log output showing the product_params map with a "photo" key mapping to a Plug.Upload struct. The struct includes content_type, filename, and path to a temporary file. ```elixir %{"title" => "Metaprogramming Elixir", "description" => "Write Less Code, Get More Done (and Have Fun!)", "price" => "15.000000", "views" => "0", "photo" => %Plug.Upload{content_type: "image/png", filename: "meta-cover.png", path: "/var/folders/_6/xbsnn7tx6g9dblyx149nrvbw0000gn/T//plug-1434/multipart-558399-917557-1"}} ``` -------------------------------- ### Generate full URL with url/1 Source: https://phoenix.hexdocs.pm/routing.html Wrap a ~p path with Phoenix.VerifiedRoutes.url/1 to get a full URL. The example shows the output for ~p"/users" as http://localhost:4000/users. ```elixir url(~p"/users") "http://localhost:4000/users" ``` -------------------------------- ### Example Product Output Source: https://phoenix.hexdocs.pm/in_context_relationships.md Shows the expected output when viewing a product with categories after the changes. ```text text Title: Elixir Flashcards Description: Flash card set for the Elixir programming language Price: 5.000000 Views: 0 Categories: Education Books ``` -------------------------------- ### Get flash values in HEEx template Source: https://phoenix.hexdocs.pm/Phoenix.Flash.md Use Phoenix.Flash.get/2 to retrieve flash values by key from the @flash map. The example shows displaying the :info and :error keys in separate divs. ```heex