Skip to content

Latest commit

 

History

History
327 lines (198 loc) · 6.35 KB

File metadata and controls

327 lines (198 loc) · 6.35 KB

Phoenix

Overview

HTMLJSONWeb Sockets
Phoenix
Database

Example flow of Phoenix app

  1. Incoming Req
  2. Ensure its an HTML Req
  3. See if it has a session
  4. Do a security check
  5. Put on HTTP headers for a browser
  6. See what the Req was trying to access
  7. Formulate and return a response
Request
v
Router
v
Controller<-Model<-Database
v
View<-Template
v
Response

MVC

Screenshot%20from%202016-12-06%2019-58-27.png

Installation

Phoenix

To install Phoenix archive

mix archive.install https://github.com/phoenixframework/archives/raw/master/phoenix_new.ez

Postgresql

For Fedora

  • sudo systemctl {restart/start/stop} postgresql.service
  • sudo postgresql_setup initdb

Create a project

  • mix phoenix.new {project name}
  • mix ecto.create Creates underlying database
  • mix phoenix.server start server
  • iex -S mix phoenix.server run app in IEx

Generators

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

Route

Connection > endpoint > router > pipeline > controller

Pipelines

pipe_through :browser

Common set of tasks for some logical group of functions

Route table

Dynamic routes

get "/hello/:name", HelloController, :world

creates a parameter called :name %{"name" => name} used in controller

Scope

Helper functions

Router helpers

{module}_path helper

  • Arg1: Conn
  • Arg2: Function
  • Arg…: Params

Config

Master configuration file config/config.exs

Environment

set via MIX_ENV

  • prod
  • dev
  • test

Test

Endpoint

Chain of functions plugs

End with router

Plug

Consumes and produces a common data structure Plug.Conn

Pipeline

Pipelines of plugs

Conn

  • assign used to pass values into template

Web

Contains all the required modules

Model

  • Ecto.Schema
  • Ecto
  • Ecto.Changeset
  • Ecto.Query only from:1 and from:2

Controller

  • Phoenix.Controller
  • App.Repo
  • Ecto
  • Ecto.Query
  • App.Router.Helpers
  • App.Gettext

View

  • Phoenix.View
  • Phoenix.Controller only get_csrf_token get_flash view_module
  • Phoenix.HTML
  • App.Router.Helpers
  • App.ErrorHelpers
  • App.Gettext

Router

  • Phoenix.Router

Channel

  • Phoenix.Channel
  • App.Repo
  • Ecto
  • Ecto.Query
  • App.Gettext

Controller

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

Common services

Action

  • 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
    • conn holds info of the request
    • params map that holds parameters passed in HTTP request

View

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

Template

Become a render(template_name, assigns) clause in respective view

def render("404.html", _assigns) do
  "Page not found"
end

Tags

<%= %> Injects result into template

<% %> Without injecting result

Helpers

Link

link "View", to: user_path(@conn, :show, user.id)

Keyword list to: sets link target

Assigns

<h1> Hello <%= String.capitalize @name %>!</h1>

Accessing name variable assigned in render called in controller

Nesting templates

<%= render "user.html", user:@user %>

Template can render another template

Layouts

When render is called in controller, layout template is rendered before actual template

Special assigns
  • @view_module
  • @view_template
  • @conn is also available in layout

Ecto Model

The raw data of the web app

mix ecto.create

Model

Schema

Specifies the underlying database table and the Elixir struct

Primary key automatically defined and default to :id

Schema

Ecto use schema to define Elixir struct

Create struct using %App.Module{}

Field

Correspond to both a field in db and in Elixir struct

Virtual field

not persisted in db

Migration

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

Changeset

Holds all the changes you want to perform on the database Encapsulates process of

  • receiving external data
  • casting
  • validating

Queries

Authentication

Channels

OTP

Umbrellas

CSS

Materializecss