| HTML | JSON | Web Sockets |
| Phoenix | ||
| Database |
Example flow of Phoenix app
- Incoming Req
- Ensure its an HTML Req
- See if it has a session
- Do a security check
- Put on HTTP headers for a browser
- See what the Req was trying to access
- Formulate and return a response
| Request | ||||
| v | ||||
| Router | ||||
| v | ||||
| Controller | <- | Model | <- | Database |
| v | ||||
| View | <- | Template | ||
| v | ||||
| Response |
To install Phoenix archive
mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez
For Fedora
sudo systemctl {restart/start/stop} postgresql.servicesudo postgresql_setup initdb
mix phoenix.new {project name}mix ecto.createCreates underlying databasemix phoenix.serverstart serveriex -S mix phoenix.serverrun app in IEx
Mix tasks used to bootstrap applications
Create simple CRUD app
phoenix.gen.json creates simple HTTP scaffold for a REST-based API using JSON
phoenix.gen.html creates simple HTTP scaffold with HTML pages
phoenix.gen.model creates just the model scaffold
Command
mix phoenix.gen.html \{Name of module that define model} \{Plural form of model name} \used for resources and schema{Each model field with type info}
Example
mix phoenix.gen.html Video videos user_id:references:users \
url:string title:string description:text
Model field can support belongs_to associations via references:
After specifying references of foreign key, has_many association has to be manually added to the referenced model
Connection > endpoint > router > pipeline > controller
pipe_through :browser
Common set of tasks for some logical group of functions
get "/hello/:name", HelloController, :world
creates a parameter called :name %{"name" => name} used in controller
{module}_path helper
- Arg1: Conn
- Arg2: Function
- Arg…: Params
Master configuration file
config/config.exs
set via MIX_ENV
- prod
- dev
- test
Chain of functions plugs
End with router
Consumes and produces a common data structure Plug.Conn
Pipelines of plugs
assignused to pass values into template
Contains all the required modules
- Ecto.Schema
- Ecto
- Ecto.Changeset
- Ecto.Query only from:1 and from:2
- Phoenix.Controller
- App.Repo
- Ecto
- Ecto.Query
- App.Router.Helpers
- App.Gettext
- Phoenix.View
- Phoenix.Controller only get_csrf_token get_flash view_module
- Phoenix.HTML
- App.Router.Helpers
- App.ErrorHelpers
- App.Gettext
- Phoenix.Router
- Phoenix.Channel
- App.Repo
- Ecto
- Ecto.Query
- App.Gettext
Connection > endpoint > router > pipeline > controller
- The controller figures out what user wants and grab the correct model, put in view and return to the user the results
- Controllers are build on Plug and are plugs themshelves
- Controller functions are called
actions - Actions are invoked from router responding to HTTP requests
- Actions gathers the required data and invoke the view layer to render a template
- Has 2 parameters
connholds info of the requestparamsmap that holds parameters passed in HTTP request
View modules are responsible for rendering. In a sense the template takes the model and make it look nice. ie parsing text for rendering
Name of view module inferred from name of controller module
UserController --> UserView
Become a render(template_name, assigns) clause in respective view
def render("404.html", _assigns) do
"Page not found"
end
<%= %> Injects result into template
<% %> Without injecting result
link "View", to: user_path(@conn, :show, user.id)
Keyword list to: sets link target
<h1> Hello <%= String.capitalize @name %>!</h1>
Accessing name variable assigned in render called in controller
<%= render "user.html", user:@user %>
Template can render another template
When render is called in controller, layout template is rendered before actual template
- @view_module
- @view_template
- @conn is also available in layout
The raw data of the web app
mix ecto.create
Specifies the underlying database table and the Elixir struct
Primary key automatically defined and default to :id
Ecto use schema to define Elixir struct
Create struct using %App.Module{}
Correspond to both a field in db and in Elixir struct
not persisted in db
Instructing db about the types of data or the tables that it has
If the structure of the db is changed a migration has to be triggered
mix ecto.gen.migration {name} generates migration file
def change do
create table(:{name of table}) do
add :{column name}, :{type}
end
mix ecto.migrate
Holds all the changes you want to perform on the database Encapsulates process of
- receiving external data
- casting
- validating
Materializecss
