Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Phoenix Framework

Phoenix Framework

The Phoenix Framework is a highly connected web framework, created by Chris McCord and relies on the Elixir created by Jose Valim.

Blog post: http://blog.maveonair.io/phoenix-framework

maveonair

May 15, 2015
Tweet

Other Decks in Programming

Transcript

  1. defmodule Blog.Router do use Phoenix.Router pipeline :browser do plug :accepts,

    ["html"] plug :fetch_session plug :fetch_flash plug :protect_from_forgery end pipeline :api do plug :accepts, ["json"] plug :Blog.Plug.Authentication end scope "/", Blog do pipe_through :browser # Use the default browser stack get "/", PageController, :index end scope "/api", Blog.Api, as: :api do pipe_through :api # Use the API stack resources "/posts", PostController, only: [:index, :show] end end
  2. CONTROLLER defmodule Blog.PostController do use Blog.Web, :controller plug :action def

    index(conn, %{"category" => category}) do posts = Blog.Post.find_by_category(category) render conn, "index.html", posts: posts end def index(conn, _params) do render conn, "index.html", posts: Blog.Post.all end def show(conn, %{"id" => id}) do post = Blog.Post.find(id) render conn, "show.html", post: post end end
  3. VIEW defmodule Blog.PostView do use Blog.Web, :view def get_date(date) do

    Ecto.Date.to_string(date) end def render("index.json", %{posts: posts}) do Poison.encode!(%{ posts: posts }) end end
  4. TEMPLATE <ul> <%= for post <- Enum.reverse(@posts) do %> <li>

    <div class="article"> <h4> <%= link post.title, to: post_path(@conn, :show, post) %> <span class="small"><%= get_date(post.date) %></span> </h4> </div> </li> <% end %> </ul>
  5. 1. DEFINE A SOCKET IN THE ROUTER defmodule EasyChat.Router do

    use Phoenix.Router socket "/ws", EasyChat do channel "rooms:*", RoomChannel end end
  6. 2. CREATE A CHANNEL defmodule EasyChat.RoomChannel do use Phoenix.Channel def

    join("rooms:lobby", _message, socket) do {:ok, socket} end def join("rooms:" <> _private_room_id, _message, _socket) do :ignore end def handle_in("new_msg", message, socket) do broadcast!(socket, "new_msg", %{username: message["username"], body: message["body"]}) {:noreply, socket} end end
  7. 3. WRITE SOME JAVASCRIPT import {Socket} from "phoenix" let messageContainer

    = $("#messages") let messageInput = $("#message-input") let username = $("#username") let socket = new Socket("/ws") socket.connect() socket.join("rooms:lobby", {}).receive("ok", channel => { messageInput.off("keypress").on("keypress", event => { if (event.keyCode == 13) { channel.push("new_msg", { username: username.val(), body: messageInput.val()}) messageInput.val("") } }) channel.on("new_msg", message => { messageContainer.append(`<br/>[@${message.username}] ${message.body}`) }) })
  8. Most good programmers do programming not because they expect to

    get paid or get adulation by the public, but because it is fun to program. — Linus Torvalds