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

Phoenix: finest Elixir MVC framework

Alessio
December 04, 2017

Phoenix: finest Elixir MVC framework

To the moon and back with an MVC framework and a spoon of tasty sugar

Alessio

December 04, 2017
Tweet

More Decks by Alessio

Other Decks in Programming

Transcript

  1. PHOENIX To the moon and back with an MVC framework

    and a spoon of tasty sugar F I N E S T E L I X I R M V C F R A M E W O R K
  2. P H O E N I X F R A

    M E W O R K We’ve had a lot of fun getting to know the Phoenix framework. Most of the web developers at thoughtbot come from a Ruby on Rails background, so the MVC patterns are second nature. T h o u g h t b o t a b o u t E l i x i r When you combine this powerful convention with the benefits of functional programming, concurrency, and performance, we’re in love.
  3. P H O E N I X F R A

    M E W O R K Introducing the building blocks W T F S H O U L D Y O U K N O W R O U T E R The entry point for everyone of our Phoenix applications C O N T R O L L E R Tell your application what to do of incoming requests V I E W Painlessly serve pages using traditional templates C H A N N E L Seamless message broadcasting thanks to websockets on steroids C O N T E X T How about generating whole pieces of your app through scaffolding?
  4. P H O E N I X F R A

    M E W O R K Plugs Just like stars and the whole universe, when we develop a Phoenix application we are leveraging the power of the Elixir ecosystem, and Plugs are all around us. Phoenix is entirely based upon Plugs, and we can plug additional functions basically inside every layer. Inside every step of our connection lifecycle we can write plugs to manipulate what travels from the endpoint to the very Phoenix’ core. def put_headers(conn, key_values) do Enum.reduce key_values, conn, fn {k, v}, conn !-> Plug.Conn.put_resp_header(conn, to_string(k), v) end end … defmodule HelloWeb.MessageController do use HelloWeb, :controller plug :put_headers, %{content_encoding: "gzip", cache_control: "max-age=3600"} plug :put_layout, "bare.html" !!... end
  5. P H O E N I X F R A

    M E W O R K Router One file to rule them all • Single source of truth • Practical orchestration of controllers • Easy API versioning • Easy contextualization with scopes • Nested resources Rails-like convention • Pipelines are cool • Concise expressive routing through resources • Resources can be configured through only: or except: • Nesting allows pipelines to be applied for multiple routes • Very nice path helpers to be used in templates
  6. P H O E N I X F R A

    M E W O R K Controller Where the business logic lives • Here we group actions • Routing-defined action calling • Ecto nesting: we interact with model • Choose wisely between actions || plugs • Possibility to render templates or maps Rails-like convention • Convention strikes again! • Opt-in auto-magical stuff like resources convention • Flashes are useful to catch exceptions • Action fallback is ❤
  7. P H O E N I X F R A

    M E W O R K View Traditional presentation layer • Auto-wiring with controllers • Nice way to model what users should see • Render templates or render raw JSON • ErrorView means easy error management Templating • .eex files get rendered • We have layouts and we can use them • Call view’s functions inside a template • Basically the same old story
  8. P H O E N I X F R A

    M E W O R K Channel Not the typical MVC neighbor • Websockets on steroids • … sort of: different transport options avail • Routing very similar to Phoenix’ router • Leveraging the true power of GenServer • Awesome syntax, both Elixir and JS side Presence is an ideal companion • Replicates information through a cluster • Very useful to “persist” distributed data • Model your clients as actors within your distributed context • Run a distributed runtime with a snap
  9. P H O E N I X F R A

    M E W O R K Scaffold a new feature with Mix • “Mother of God” reactions • Just a mix phx.gen.html away • Helps us a lot during the design phase • You can choose wether use it or not • Didn’t dive in so much Build an interface painlessly • … sort of: it’s not a thing if you’re writing a SPA • You lose control but you gain a huge speed-up • The CRUD part goes away in minutes • Your product owner will love this part
  10. P H O E N I X F R A

    M E W O R K S O U R C E D I R A S S E T S L I B C O N F I G P R I V T E S T R O U T E R C O N T R O L L E R S M O D E L S V I E W S / T E M P L AT E S M I X TA S K S
  11. P H O E N I X F R A

    M E W O R K Ecto appreciation moment
  12. P H O E N I X F R A

    M E W O R K Brunch builds the frontend: It’s not webpack but still
  13. P H O E N I X F R A

    M E W O R K Expanding your collection of Mix tasks is easy peasy (and you’ll desperately need that)
  14. P H O E N I X F R A

    M E W O R K The best thing your potion can do is exploding in your hands, so you’ll have a chance to learn something new. ALESSIO BIANCALANA S O F T W A R E C R A F T S M A N P A R T - T I M E A L C H E M I S T
  15. P H O E N I X F R A

    M E W O R K @dottorblaster [email protected] ALESSIO BIANCALANA S O F T W A R E C R A F T S M A N P A R T - T I M E A L C H E M I S T