Phoenix - 基於Elixir的下一代Web開發框架
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.
總結
Phoenix作為Meteor社群廣泛討論的競爭對手或替代品勢必將得到關注。但這個並不僅僅是Phoenix VS Meteor的事,而是Erlang VS NodeJS的事。學好JavaScript和NodeJS,我們可以開發從前端到後端,從移動端到桌面的任何應用。所以,當前我認為若想成為全棧開發者,還是先打好JavaScript和NodeJS的基礎,然後學習Meteor和Electron,這樣是通吃三端的最高效的方式。以後區域性的優化可能會用到Erlang和Phoenix。
相關文章
- 基於Elixir使用Phoenix建立CQRS/ES應用
- 下一代基於Koa的NodeJS全棧開發框架NodeJS全棧框架
- 基於EF6的快速開發Web框架——Swift.NetWeb框架Swift
- 基於Gin框架的web後端開發(八): Gin框架的請求重定向框架Web後端
- NotaddBeta2fix1,基於Laravel的下一代PHP開發框架LaravelPHP框架
- 基於mui的H5套殼APP開發web框架分享UIH5APPWeb框架
- 基於GORM框架的web後端開發(一): GORM連線MySQLGoORM框架Web後端MySql
- 分享一個基於 Node.js 的 Web 開發框架 – NokitjsNode.jsWeb框架
- 用於Web開發的5種機器學習框架Web機器學習框架
- Notadd Beta3 釋出 (基於 Laravel 的下一代 PHP 開發框架)LaravelPHP框架
- 基於gin的golang web開發:路由GolangWeb路由
- 基於gin的golang web開發:dockerGolangWebDocker
- Toro - 基於PHP的REST開發框架PHPREST框架
- 基於Gin框架的web後端開發(七): Gin框架的檔案上傳詳解框架Web後端
- 基於Gin框架的web後端開發(二): JSON資料生成框架Web後端JSON
- 分享剛出爐的基於Blazor技術的Web應用開發框架BlazorWeb框架
- [Web框架]Spray基於Scala的REST框架SprayWeb框架REST
- 下一代Spring框架將基於JDK17和JakartaEE開發Spring框架JDK
- Notadd Beta3 fix1 (基於 Laravel 的下一代 PHP 開發框架)LaravelPHP框架
- 基於Feature Flag的下一代開發模式模式
- 基於 Web 引擎擴充套件技術的 RTC 混合開發框架實踐Web套件框架
- 基於Gin框架的web後端開發(三): 獲取queryString引數框架Web後端
- 基於gin的golang web開發:路由二GolangWeb路由
- 10個用於Web開發的最好 Python 框架WebPython框架
- 基於Gin框架的web後端開發(四): 獲取FORM表單引數框架Web後端ORM
- 基於NodeJS的14款Web框架NodeJSWeb框架
- 基於業務的Web應用框架 SimpleFrameworkWeb框架Framework
- 開發數學系統時,需要掌握的幾個基於Web的數學框架Web框架
- 《Flask Web開發 基於Python的Web應用開發實戰》簡評FlaskWebPython
- 基於gin的golang web開發:中介軟體GolangWeb
- 基於gin的golang web開發:模型繫結GolangWeb模型
- 基於gin的golang web開發:模型驗證GolangWeb模型
- 基於weex的有贊無線開發框架框架
- oo設計與基於框架開發的矛盾框架
- 基於GO語言框架Gin開發的MVC輪子框架:GinLaravelGo框架MVCLaravel
- 基於gin的golang web開發:服務間呼叫GolangWeb
- 基於gin的golang web開發:整合swaggerGolangWebSwagger
- 基於gin的golang web開發:認證利器jwtGolangWebJWT