Phoenix - 基於Elixir的下一代Web開發框架

weixin_34337265發表於2015-12-14

Phoenix概述

Phoenix是一款使用Elixir編寫的Web開發框架。它實現了服務端的MVC架構。它的許多元件和概念類似於我們常見的框架如Django和Ruby on Rails。

Phoenix提供了開發和生成環境的最佳實踐,即高生產效率和高應用效能。它同時也有一些有趣的特性,如使用channels來實現實時特性和預編譯的模板。

如果你已經對Elixir很熟悉,那麼你可以直接開始Phoenix的學習,如果不是,你也可以查閱Elixir guides來學習它。我們有一系列的幫助資源Learning Elixir and Erlang Guide

這個引導教程的目的是給你一個簡明的Phoenix概覽。包括了Phoenix的組成部分和支援Phoenix的不同層級。

Phoenix

Phoenix可以看做一系列模組組成的多層系統。其他層級包括Plug,Ecto等。Erlang的HTTP伺服器Cowboy作為Phoenix和Plug的底層基礎,這裡Phoenix將Cowboy封裝好供大家輕鬆使用。

Phoenix由一系列不同部分構成,每部分都有它的目的。這裡是一個概覽:

  • The Endpoint(入口)

    • handles all aspects of requests up until the point where the router takes over
    • provides a core set of plugs to apply to all requests
    • dispatches requests into a designated router
  • The Router(路由)

    • parses incoming requests and dispatches them to the correct controller/action, passing parameters as needed
    • provides helpers to generate route paths or urls to resources
    • defines named pipelines through which we may pass our requests
    • Pipelines
      *allow easy application of groups of plugs to a set of routes
  • Controllers(控制器)

    • provide functions, called actions, to handle requests
    • Actions
      • prepare data and pass it into views
      • invoke rendering via views
      • perform redirects
  • Views(檢視)

    • render templates
    • act as a presentation layer
    • define helper functions, available in templates, to decorate data for presentation
  • Templates(模板)

    • are what they sound like :)
    • are precompiled and fast
  • Channels(頻道)

    • manage sockets for easy realtime communication
    • are analogous to controllers except that they allow bi-directional communication with persistent connections
  • PubSub(釋出訂閱)

    • underlies the channel layer and allows clients to subscribe to topics
    • abstracts the underlying pubsub adapter for third-party pubsub integration

Plug(外掛)

Plug is a specification for constructing composable modules to build web applications. Plugs are reusable modules or functions built to that specification. They provide discrete behaviors - like request header parsing or logging. Because the Plug API is small and consistent, plugs can be defined and executed in a set order, like a pipeline. They can also be re-used within a project or across projects.

Plugs can be written to handle almost anything, from authentication to parameter pre-processing, and even rendering.

Phoenix takes great advantage of Plug in general - the router and controllers especially so.

One of the most important things about Plug is that it provides adapters to HTTP servers which will ultimately deliver application content to our users. Currently Plug only provides an adapter forCowboy, a web server written in Erlang by Loïc Hoguin of 99s.
Have a look at the Plug Guide for more details.

Ecto(類似ORM庫,類似LINQ)

Ecto is a language integrated query composition tool and database wrapper for Elixir. With Ecto, we can read and write to different databases, model our domain data, write complex queries in a type-safe way, protect against attack vectors - including SQL injection, and much more.
Ecto is built around four main abstractions:
Repo - A repository represents a connection to an individual database. Every database operation is do,lei'sne via the repository.

Model - Models are our data definitions. They define table names and fields as well as each field's type. Models also define associations - the relationships between models.

Query - Queries tie both models and repositories together, allowing us to elegantly retrieve data from the repository and cast it into the models themselves.

Changeset - Changesets declare transformations we need to perform on our model data before our application can use it. These include type casting, validations, and more.

A new Phoenix application will use Ecto with PostgreSQL storage by default.

551828-b9d5a0d061f7dbe0.png
Phoenix Request流程圖

總結

Phoenix作為Meteor社群廣泛討論的競爭對手或替代品勢必將得到關注。但這個並不僅僅是Phoenix VS Meteor的事,而是Erlang VS NodeJS的事。學好JavaScript和NodeJS,我們可以開發從前端到後端,從移動端到桌面的任何應用。所以,當前我認為若想成為全棧開發者,還是先打好JavaScript和NodeJS的基礎,然後學習Meteor和Electron,這樣是通吃三端的最高效的方式。以後區域性的優化可能會用到Erlang和Phoenix。

相關文章