|
1 | 1 | defmodule PhxExampleWeb do
|
2 |
| - def controller do |
| 2 | + @moduledoc """ |
| 3 | + The entrypoint for defining your web interface, such |
| 4 | + as controllers, components, channels, and so on. |
| 5 | +
|
| 6 | + This can be used in your application as: |
| 7 | +
|
| 8 | + use PhxExampleWeb, :controller |
| 9 | + use PhxExampleWeb, :html |
| 10 | +
|
| 11 | + The definitions below will be executed for every controller, |
| 12 | + component, etc, so keep them short and clean, focused |
| 13 | + on imports, uses and aliases. |
| 14 | +
|
| 15 | + Do NOT define functions inside the quoted expressions |
| 16 | + below. Instead, define additional modules and import |
| 17 | + those modules here. |
| 18 | + """ |
| 19 | + |
| 20 | + def static_paths, do: ~w(assets fonts images favicon.ico robots.txt) |
| 21 | + |
| 22 | + def router do |
3 | 23 | quote do
|
4 |
| - use Phoenix.Controller, namespace: PhxExampleWeb |
| 24 | + use Phoenix.Router, helpers: false |
5 | 25 |
|
6 | 26 | import Plug.Conn
|
7 |
| - alias PhxExampleWeb.Router.Helpers, as: Routes |
| 27 | + import Phoenix.Controller |
| 28 | + import Phoenix.LiveView.Router |
8 | 29 | end
|
9 | 30 | end
|
10 | 31 |
|
11 |
| - def view do |
| 32 | + def channel do |
12 | 33 | quote do
|
13 |
| - use Phoenix.View, |
14 |
| - root: "lib/phx_example_web/templates", |
15 |
| - namespace: PhxExampleWeb |
| 34 | + use Phoenix.Channel |
16 | 35 | end
|
17 | 36 | end
|
18 | 37 |
|
19 |
| - def router do |
| 38 | + def controller do |
20 | 39 | quote do
|
21 |
| - use Phoenix.Router |
| 40 | + use Phoenix.Controller, |
| 41 | + formats: [:html, :json], |
| 42 | + layouts: [html: PhxExampleWeb.Layouts] |
22 | 43 |
|
23 | 44 | import Plug.Conn
|
24 |
| - import Phoenix.Controller |
| 45 | + |
| 46 | + unquote(verified_routes()) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + def live_view do |
| 51 | + quote do |
| 52 | + use Phoenix.LiveView, |
| 53 | + layout: {PhxExampleWeb.Layouts, :app} |
| 54 | + |
| 55 | + unquote(html_helpers()) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def live_component do |
| 60 | + quote do |
| 61 | + use Phoenix.LiveComponent |
| 62 | + |
| 63 | + unquote(html_helpers()) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def html do |
| 68 | + quote do |
| 69 | + use Phoenix.Component |
| 70 | + |
| 71 | + import Phoenix.Controller, |
| 72 | + only: [get_csrf_token: 0, view_module: 1, view_template: 1] |
| 73 | + |
| 74 | + unquote(html_helpers()) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + defp html_helpers do |
| 79 | + quote do |
| 80 | + import Phoenix.HTML |
| 81 | + |
| 82 | + alias Phoenix.LiveView.JS |
| 83 | + |
| 84 | + unquote(verified_routes()) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def verified_routes do |
| 89 | + quote do |
| 90 | + use Phoenix.VerifiedRoutes, |
| 91 | + endpoint: PhxExampleWeb.Endpoint, |
| 92 | + router: PhxExampleWeb.Router, |
| 93 | + statics: PhxExampleWeb.static_paths() |
25 | 94 | end
|
26 | 95 | end
|
27 | 96 |
|
| 97 | + @doc """ |
| 98 | + When used, dispatch to the appropriate controller/view/etc. |
| 99 | + """ |
28 | 100 | defmacro __using__(which) when is_atom(which) do
|
29 | 101 | apply(__MODULE__, which, [])
|
30 | 102 | end
|
|
0 commit comments